2016-12-17 6 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> 
関連する問題