第二十四天 框架之痛-Spring MVC(四)
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>
<?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>
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
項目結構圖
第二十四天 框架之痛-Spring MVC(四)