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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- spring MVC中Action使用的配置--> <!-- Action类可继承ActionController实现基本页面跳转-->
<!-- Action类可继承SimpleFormController实现接收表单后的跳转-->
<bean name="/hbtAccount.html" class="hibntTest4.action.AccountAction"></bean>
<bean name="/hbtInfoSet.html" class="hibntTest4.action.InfoSetAction">
<!-- 配置接收表单的类-->
<property name="commandClass">
<value>hibntTest4.domain.User</value>
</property>
</bean>
</beans>
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml的位置,例如:/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Action书写如下:
public class AccountAction extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
return new ModelAndView("/WEB-INF/hibnt4/account.jsp");
}
}
InfoSetAction书写如下
public class InfoSetAction extends SimpleFormController{
@Override
protected ModelAndView onSubmit(Object form) throws Exception {
//接收来自页面传来的表单信息,并自动封装为User类型的对象
User u=(User)form;
//根据用户名查找数据库得到用户信息
User user=((UserDao)ACUtil.getAC().getBean("userDao")).findByName(u.getUsername());
if(user!=null){
if(user.getPassword().equals(u.getPassword())){
//如果用户名和密码都正确,返回用户信息,可通过EL表达试得到相应值,如:${user.username }
return new ModelAndView("/WEB-INF/hibnt4/showInfo.jsp","user",user);
}else{
//如果用户名存在,但密码不正确,返回到登录页面,并告知错误信息
return new ModelAndView("/WEB-INF/hibnt4/account.jsp","error","用户名或密码不正确!");
}
}else{
return new ModelAndView("/WEB-INF/hibnt4/infoSet.jsp","user",u);
}
}
}