我正在使用Springs表单标记库来提交用户表单..我能够在方法处理程序中捕获post请求参数,但它不会显示在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>
请参阅输出的截图
答案 0 :(得分:1)
您已将实体User
绑定到名为user
的键,因此当您想要获取对象值时,您应该使用此键${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>