2016-12-17 4 views
0

JSP로 간단한 등록 양식 페이지를 만들고 싶습니다. 나는 봄에 새로운 사람입니다. 나는 JSP 페이지에 값을 넣으면 다른 JSP 페이지가 그 값을 가지고 보여주기 위해 노력하고있다.JSP 페이지가 다른 JSP 페이지의 값을 전달할 수없는 이유는 무엇입니까?

public class LoginController extends HttpServlet{ 


    @RequestMapping(value="/user",method = RequestMethod.GET) 
     /* public String viewRegistration(Map<String, Object> model) { 
      User userForm = new User();  
      model.put("userForm", userForm); 
      return "index"; 
     }*/ 

     public ModelAndView student() { 

      return new ModelAndView("user", "user", new User()); 
     } 

     @RequestMapping(value="/Login",method = RequestMethod.POST) 
     public String processCredentials(@RequestParam("fullName")String fullName,@RequestParam("fName")String fName 
               ,@RequestParam("mName")String mName,@RequestParam("nationality")String nationality 
               ,@RequestParam("occupation")String occupation,@RequestParam("address")String address 
               ,@RequestParam("about")String about , HttpServletRequest request, 
               HttpServletResponse response, Object command , @ModelAttribute("user") User user, 
               ModelMap model) { 
      String message = "Invalid credentials"; 
      /*if(userName.equals("admin") && password.equals("123")) { 

        message = "Welcome " + userName + "!!"; 

      }*/ 
     /* Map<String,String>data = new HashMap<String,String>(); 
      data.put("fullName",fullName); 
      data.put("fName",fName); 
      data.put("mName",mName); 
      data.put("nationality",nationality); 
      data.put("occupation",occupation); 
      data.put("address",address); 
      data.put("about",about); 
      */ 
      request.getAttribute(fullName); 
      request.getAttribute(fName); 
      request.getAttribute(mName); 
      request.getAttribute(nationality); 
      request.getAttribute(occupation); 
      request.getAttribute(address); 
      request.getAttribute(about); 
      model.addAttribute(user.getFullName()); 
      model.addAttribute(user.getfName()); 
      model.addAttribute(user.getmName()); 
      model.addAttribute(user.getfName()); 
      model.addAttribute(user.getNationality()); 
      model.addAttribute(user.getAddress()); 
      model.addAttribute(user.getAbout()); 
      model.addAttribute(user.getOccupation()); 

      //User user2 = new User(); 
       model.addAttribute("user", user); 
       return "welcome"; 

      // return new ModelAndView("welcome" , "request",request); 

     } 

내 index.jsp를은 다음과 같습니다 :

<!-- <html> 

<head> <title>Spring MVC login example.</title> 
</head> 
<body> 
<form action="login" method="post"> 
UserName:<input type="text" name="userName"/> <br/><br/> 
Password:<input type="password" name="password"/> <br/><br/> 
<input type="submit" value="login"/> 
</form> 
</body> 
</html> --> 
<%-- 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Spring Login Form</title> 
</head> 
<body> 
    <form action="login" method="POST"> 
     <div align="center"> 
      <table> 
       <tr> 
        <td>User Name</td> 
        <td><input type="text" name="userName" /></td> 
       </tr> 
       <tr> 
        <td>Password</td> 
        <td><input type="password" name="password" /></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type="submit" value="Submit" /></td> 
       </tr> 
      </table> 
      <c:choose> 
      <c:when test="${request.getAttribute("message")!='null'"}"> 
      <%= request.getAttribute("message") %> 
      </c:when> 
      </c:choose> 
     </div> 
    </form> 
</body> 
</html> 


--%> 

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<style> 
td{ 
    clear: left; 
    width: 100px; 
    text-align: right; 
    margin-right: 10px; 
    font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:14px; 
    color:red; 
    } 

    body { 
    background-color:#b6e1a0; 

} 
</style> 
<title>Spring Login Form</title> 
</head> 
<body> 
    <form action="/LoginSystemPAL/Login" method="POST" commandName="user" > 
     <div align="center"> 
      <table> 
       <tr> 
        <td>Full Name</td> 
        <td><input type="text" name="fullName" path="fullName" /></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Father's Name</td> 
        <td><input type="text" name="fName" path="fName"/></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Mother's Name</td> 
        <td><input type="text" name="mName" path="mName" /></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Nationality</td> 
        <td><input type="text" name="nationality" path="nationality"/></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Occupation</td> 
        <td><input type="text" name="occupation" path="occupation"/></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
        <tr> 
        <td>Address</td> 
        <td><input type="text" name="address" path="address"/></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>About</td> 
        <td><input type="text" name="about" path="about" /></td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type="submit" value="Submit" /></td> 
       </tr> 
      </table> 

     </div> 
    </form> 
</body> 
</html> 

내 위해 welcome.jsp은 다음과 같습니다

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<style> 
td{ 
    clear: left; 
    width: 100px; 
    text-align: right; 
    margin-right: 10px; 
    font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:14px; 
    color:red; 
    } 

    body { 
    background-color:#b6e1a0; 

} 
</style> 
<title>Spring Login Form</title> 
</head> 
<body> 

     <div align="center"> 
      <table> 
       <tr> 
        <td>Full Name</td> 
        <td> <%=request.getAttribute("user.fullName")%></td> 
       <%-- <td> ${fullName} </td> --%> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Father's Name</td> 
        <%-- <td><%=request.getAttribute("fName")%></td> --%> 
        <td> ${user.fName} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Mother's Name</td> 
        <%-- <td><%=request.getAttribute("mName")%></td> --%> 
        <td> ${mName} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Nationality</td> 
        <%-- <td><%=request.getAttribute("nationality")%></td> --%> 
         <td> ${nationality} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>Occupation</td> 
        <%-- <td><%=request.getAttribute("occupation")%></td> --%> 
        <td> ${occupation} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
        <tr> 
        <td>Address</td> 
        <%-- <td><%=request.getAttribute("address")%></td> --%> 
        <td> ${address} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td>About</td> 
        <%-- <td><%=request.getAttribute("about")%></td> --%> 
        <td> ${about} </td> 
       </tr> 
       <tr> 
        </br></br></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type="submit" value="Submit" /></td> 
       </tr> 
      </table> 

     </div> 

</body> 
</html> 

내 pom.xml 파일은 다음과 같습니다

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
     <modelVersion>4.0.0</modelVersion> 
     <groupId>com.pran.pal</groupId> 
     <artifactId>LoginSystemPAL</artifactId> 
     <packaging>war</packaging> 
     <version>0.0.1-SNAPSHOT</version> 
     <name>LoginSystemPAL Maven Webapp</name> 
     <url>http://maven.apache.org</url> 
     <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>4.3.4.RELEASE</version> 
     </dependency> 
     <dependency> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-resources-plugin</artifactId> 
     <version>2.4.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>3.1.0.RELEASE</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>3.1.0.RELEASE</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>taglibs</groupId> 
      <artifactId>standard</artifactId> 
      <version>1.1.2</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>3.0-alpha-1</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     </dependencies> 
     <build> 
     <finalName>LoginSystemPAL</finalName> 
     </build> 
    </project> 


The web.xml is: 

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 

    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.css</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.js</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.png</url-pattern> 
</servlet-mapping> 

</web-app> 
<!-- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
    version="2.4"> 
    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

</web-app> --> 
여기 내 LoginController.java입니다

welcomre.jsp 값을 얻지 못했습니다. 다음과 같이 표시됩니다. this is the welcomee.jsp

지금은 무엇을 할 수 있습니까?

답변

0

${modelAttribute} 대신 <c:out value="${modelAttribute}" />을 사용해 보시고 효과가 있는지보십시오.

아직 언급하지 않은 경우 jstl 용 taglib가 포함되어 있습니다. 에서 스크립틀릿을 사용하지 않는, 둘째

/** 
* Spring MVC framework will automatically bind form fields with name X to corresponding 
* fields name setX() in specified Model attribute. 
* 
*/ 
@RequestMapping(value="/Login",method = RequestMethod.POST) 
public String processCredentials(@ModelAttribute("user") User user) { 

    //make Model Attribute available to next view 
    return new ModelAndView("welcome", user); 
} 

:

<%@ taglib prefix="c" 
     uri="http://java.sun.com/jsp/jstl/core" %> 
0

나는 다음과 같이 컨트롤러 방법에 RedirectAttributes을 추가하여 flash attribute facility of Spring MVC을 사용하는 것이 좋습니다 것

@RequestMapping(value="/Login",method = RequestMethod.POST) 
     public String processCredentials(@RequestParam("fullName")String fullName, 
       @RequestParam("fName")String fName, 
       @RequestParam("mName")String lName, 
       @RequestParam("nationality")String nationality, 
       @RequestParam("occupation")String occupation, 
       @RequestParam("address")String address, 
       @RequestParam("about")String about, 
       HttpServletRequest request, 
       HttpServletResponse response, 
       Object command, 
       @ModelAttribute("user") User user, 
       ModelMap model, 
       RedirectAttributes redir) { 



      redir.addFlashAttribute("user", user); 

      return "redirect:/welcome"; 
     } 
0

첫째 컨트롤러를 단순화 JSP 페이지. 당신의 JSP에서

How to avoid Java code in JSP files?

당신은 다음 JSP 표현 언어 또는 JSTL 태그를 사용할 수 있습니다 : EL 여기 괜찮습니다. 패턴은 다음과 같습니다.

<tr> 
    <td> ${user.about} </td> 
</tr> 
관련 문제