2016-04-29 18 views
0

私はユーザーフォームを送信するためにSpringsフォームタグライブラリを使用しています。メソッドハンドラでPOSTリクエストパラメータを捕まえることができますが、印刷する必要があるjspに表示されませんどんなユーザーは以下 投稿要求パラメータがjspに表示されない

は、コントローラクラスに..です提出した

package com.sagar; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 


@Controller 

public class MyController { 



    @RequestMapping(value="/edit",method=RequestMethod.GET) 

    public String handleUserInput(Model m,@ModelAttribute("userLogin") User user){ 

     m.addAttribute(new User()); 

     return "edit"; 

    } 


    @RequestMapping(value="/edit",method=RequestMethod.POST) 

    public String handleUserPost(@ModelAttribute("userLogin") User user,Model mav){ 
     mav.addAttribute("user",user); 
     System.out.println(user.getFirstName()); 
     System.out.println(user.getLastName()); 
     System.out.println(user.getPassword()); 

     return "view"; 

    } 

} 

のWebコンテキストの下

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd "> 


    <context:component-scan base-package="com.sagar"/> 

<bean 
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix"><value>/WEB-INF/</value></property> 
<property name="suffix"><value>.jsp</value></property> 
<property name="viewClass"> 
<value>org.springframework.web.servlet.view.JstlView</value> 
</property> 
</bean> 
<mvc:default-servlet-handler/> 
<mvc:annotation-driven/> 
</beans> 

は私のユーザークラス

です
package com.sagar; 

public class User { 

    private String firstName; 
    private String lastName; 
    private String password; 

    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

} 

View.jsp(どのユーザーからでも送信されます)。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 
    pageEncoding="ISO-8859-1"%> 
 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
 
    <%@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=ISO-8859-1"> 
 
<title>Insert title here</title> 
 
</head> 
 
<body> 
 
<table> 
 
    <tr> 
 
     <td>Name</td> 
 
     <td>${firstName}</td> 
 
    </tr> 
 
    <tr> 
 
     <td>LastName</td> 
 
     <td>${lastName}</td> 
 
    </tr> 
 
    <tr> 
 
     <td>password</td> 
 
     <td>${password}</td> 
 
    </tr> 
 
</table> 
 
</body> 
 
</html>

ユーザーフォーム(たedit.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 
    pageEncoding="ISO-8859-1"%> 
 
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/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=ISO-8859-1"> 
 
<title>Welcome to Login Page</title> 
 
</head> 
 
<body> 
 

 
<form:form modelAttribute="userLogin"> 
 
     <table> 
 
      <tr> 
 
       <td>First Name:</td> 
 
       <td><form:input path="firstName" /></td> 
 
      </tr> 
 
      <tr> 
 
       <td>Last Name:</td> 
 
       <td><form:input path="lastName" /></td> 
 
      </tr> 
 
      <tr> 
 
     <td>Password:</td> 
 
     <td> 
 
      <form:password path="password" /> 
 
     </td> 
 
    </tr> 
 
      <tr> 
 
       <td colspan="2"> 
 
        <input type="submit" value="Log In" /> 
 
       </td> 
 
      </tr> 
 
     </table> 
 
    </form:form> 
 
</body> 
 
</html>

出力のこのスクリーンショットを参照してください
Please see this screenshot of output

答えて

1

エンティティUseruserという名前のキーにバインドしました。したがって、Object値を取得する場合は、このキー${user.firstName}を使用する必要があります。
jspをレンダリングするためのパラメータを変更してください;

<table> 
    <tr> 
    <td>Name</td> 
    <td>${user.firstName}</td> 
    </tr> 
    <tr> 
    <td>LastName</td> 
    <td>${user.lastName}</td> 
    </tr> 
    <tr> 
    <td>password</td> 
    <td>${user.password}</td> 
    </tr> 
</table> 
関連する問題