我看了几乎所有的答案,但我找不到我的代码中的问题是什么。你能帮我一个忙吗?
这是我的JSp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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>Feedback Page</title>
</head>
<body>
<form:form method="POST" commandName="userForm">
<form:errors/>
<table>
<tr>
<td>Name :</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Profession:</td>
<td><form:radiobutton path="profession" value="Student" label="Student" />
<form:radiobutton path="profession" value="Programmer" label="Programmer" /></td>
</tr>
<tr>
<td>mailinglist:</td>
<td><form:input path="mailinglist" /></td>
</tr>
<tr>
<td>Country :</td>
<td><form:select path="country">
<form:option value="0" label="Select" />
<form:option value="India" label="India" />
<form:option value="United States" label="United States" />
<form:option value="United Kingdom" label="United Kingdom" />
<form:option value="Europe" label="Europe" />
<form:option value="Canada" label="Canada" />
</form:select></td>
</tr>
<tr>
<td>Feedback:</td>
<td><form:textarea path="feedback" /></td>
</tr>
<tr>
<td>Frameworks:</td>
<td>
<form:checkbox path="frameworks" value="Spring" label="Spring" />
<form:checkbox path="frameworks" value="Hibernate" label="Hibernate" />
<form:checkbox path="frameworks" value="Struts" label="Struts" />
<form:checkbox path="frameworks" value="DOJO" label="DOJO" />
</td>
</tr>
<tr>
<td></td>
<td><form:checkbox path="mailingList"
label="Would you like to join our mailinglist?" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<!--<welcome-file>redirect.jsp</welcome-file>-->
<welcome-file>/WEB-INF/jsp/user.jsp</welcome-file>
</welcome-file-list>
</web-app>
这是我的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean class="com.test.UserController" >
<property name="pages">
<list>
<value>user</value> <!-- page1 -->
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
这是我的user.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test;
public class User {
private String username;
private String password;
private String remark;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the remark
*/
public String getRemark() {
return remark;
}
/**
* @param remark the remark to set
*/
public void setRemark(String remark) {
this.remark = remark;
}
}
这是我的控制器类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
public class UserController extends AbstractWizardFormController{
public UserController()
{
setCommandClass(User.class);
setCommandName("userForm");
}
@Override
protected ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response, Object o, BindException be) throws Exception {
User user=(User)o;
System.out.println("#####################User :"+user);
return new ModelAndView("success","user",user);
}
}
实际Bean: public class User {
private String username;
private String profession;
private String country;
private String feedback;
private String[] frameworks;
private boolean mailinglist;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the profession
*/
public String getProfession() {
return profession;
}
/**
* @param profession the profession to set
*/
public void setProfession(String profession) {
this.profession = profession;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(String country) {
this.country = country;
}
/**
* @return the feedback
*/
public String getFeedback() {
return feedback;
}
/**
* @param feedback the feedback to set
*/
public void setFeedback(String feedback) {
this.feedback = feedback;
}
/**
* @return the frameworks
*/
public String[] getFrameworks() {
return frameworks;
}
/**
* @param frameworks the frameworks to set
*/
public void setFrameworks(String[] frameworks) {
this.frameworks = frameworks;
}
/**
* @return the mailinglist
*/
public boolean isMailinglist() {
return mailinglist;
}
/**
* @param mailinglist the mailinglist to set
*/
public void setMailinglist(boolean mailinglist) {
this.mailinglist = mailinglist;
}
}
提前感谢!
在您的模型中为JSP的URL添加新方法
@Override
protected ModelAndView processStart(HttpServletRequest request, HttpServletResponse response, BindException be) throws Exception {
User user=new User();
return new ModelAndView("UserJSPname","userForm",user);
}
它基本上初始化空User对象和设置为model。