问下大家怎么用这个类啊?我编写了一个例子,但不太理解:
UserController.java:
package src;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
public class UserController extends AbstractCommandController{
public UserController(){
setCommandClass(User.class);
}
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException exception) throws Exception{
User user=(User) command;
name=user.getName();
String nameAndPassword=name+" "+user.getPassword();
return new ModelAndView("User","userinfo",nameAndPassword);
}
private String name;
private String password;
public void setName(String name){
this.name=name;
}
public void setPassword(String password){
this.password=password;
} //红色部分是必需的吗?还是可以不要?
}
User.java:
package src;
public class User {
private String name;
private String password;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="user" class="src.UserController">
</bean>
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/aaa.htm">aaa</prop>
<prop key="/home.htm">home</prop>
<prop key="/course.htm">courseService</prop>
<prop key="/user.htm">user</prop>
</props>
</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>
最后要显示的View(User.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Information</title>
</head>
<body>
<h2>${userinfo }</h2>
</body>
</html>
问题就是,当我在地址栏里面输入user.htm的时候,显示出来的是2个null。怎么使用AbstractCommandController来传递参数啊?用这个类有什么好处?还有就是红色字体的部分的问题。谢谢大家耐心点看啊!