6月3日,晴。“绿树浓阴夏日长。 楼台倒影入池塘。 水晶帘动微风起, 满架蔷薇一院香”。
以用户注冊过程为例。我们可能会选择继承AbstractController来实现表单的显示。
1、导入spring-framework-2.5.6的jar包,详见第八天
框架之痛-Spring MVC(一)。
2、编写web.xml 的配置文件。主要增加spring Web MVC框架提供的
org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题。
<? xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>myOneMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myOneMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、myOneMVC-servlet.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean name="/reg" class="edu.eurasia.controller.RegControl"></bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4、后端控制器RegControl.java
package edu.eurasia.controller; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; public class RegControl extends SimpleFormController { public RegControl() { setCommandClass(User.class); } @Override protected ModelAndView onSubmit(Object object, BindException errors) throws Exception { User user=(User)object; ModelAndView mav = new ModelAndView("hello"); mav.addObject("user", user); return mav; } }
RegControl.java继承SimpleFormController类,构造器中调用setCommandClass方法绑定定命令对象(这里为User类),转换object为User类进行业务逻辑操作。onSubmit()方法能够返回一个ModelAndView对象,完毕向页面输出数据的功能。
5、表单相应的POJO-User.java
package edu.eurasia.controller; public class User { private String username; private int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
6、表单填写页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="reg" method="post"> 用户名:<input type="text" name="username"><br/> 年龄:<input type="text" name="age"><br/> <input type="submit"> </form> </body> </html>
7、返回一个对应的给client页面hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> Brazil世界杯,你好!<br/> 用户名:${user.username }<br/> 年龄:${user.age } </body> </html>
8、执行Tomcat 7 。输入http://localhost:8080/springMVC_controllerweb_1/index.jsp
项目结构图
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvendzendz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
时间: 2024-10-01 20:58:12