若是每次傳入參數都使用HttpServletRequest獲取頁面元素的值,假如要獲取的頁面元素值不多時,能夠這樣作,若是頁面元素不少,就要使用不少的HttpServletRequest.getParameter(),顯得很繁瑣。爲了解決這個問題,Spring提供了表單控制器SimpleFormController,把頁面form中的元素名稱設定爲和Bean中的同樣,傳入的時候,Spring就會自動抓取form中和Bean名稱同樣的元素值,把它轉換成一個Bean,使開發人員能夠方便地使用。 html
Project的目錄結構以下: java
(1)提交form的頁面input.jsp web
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %> < html > < head > < title > 第一個SpringMVC實例 </ title > </ head > <% String str = ( String ) request.getAttribute( " helloWorld " ); %> < body > < form name ="HelloWorld" action ="/myApp/helloWorld.do" method ="post" > 歡迎語 < input name ="msg" type ="text" /> < br /> < input type ="submit" value ="提交" /> </ form > </ body > </ html >
(2)輸出提交內容的頁面show.jsp spring
(3)用來存放提交內容更的類HelloWorld.java,注意它有個字段private String msg,是與input.jsp中form表單裏的元素名稱一致的 apache
(4)Web.xml的配置與上一篇文章中提到的同樣,這裏須要修改dispatcherServlet-servlet.xml的內容 瀏覽器
<? 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 ="urlMapping" class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > < property name ="mappings" > < props > < prop key ="helloWorld.do" > helloWorldAction </ prop > </ props > </ property > </ bean > <!-- 定義視圖 及JSP路徑 --> < bean id ="viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name ="viewClass" > < value > org.springframework.web.servlet.view.InternalResourceView </ value > </ property > <!-- 定義JSP存放的路徑 --> < property name ="prefix" > < value > /jsp/ </ value > </ property > <!-- 定義JSP頁面後綴 --> < property name ="suffix" > < value > .jsp </ value > </ property > </ bean > <!-- 定義控制器 --> < bean id ="helloWorldAction" class ="com.jp.action.SimpleFormControllerAction" > < property name ="commandClass" > < value > com.jp.action.HelloWorld </ value > </ property > < property name ="viewPage" > < value > show </ value > </ property > </ bean > </ beans >
(5)修改控制器代碼,使其繼承SimpleFormController,SimpleFormControllerAction.java代碼以下 mvc
package com.jp.action; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; public class SimpleFormControllerAction extends SimpleFormController{ private Logger logger = Logger.getLogger( this .getClass().getName()); private String viewPage; public String getViewPage() { return viewPage; } public void setViewPage(String viewPage) { this .viewPage = viewPage; } // 重寫onSubmit方法 public ModelAndView onSubmit(Object command) throws Exception{ HelloWorld helloWorld = (HelloWorld)command; Map model = new HashMap(); // new String...是用來解決中文亂碼問題 model.put( " yourWords " , new String(helloWorld.getMsg().getBytes( " iso-8859-1 " ), " utf-8 " )); // 返回依賴注入定義的頁面 return new ModelAndView(getViewPage(),model); } }
最後,在瀏覽器中打開http://localhost:8080/myApp/jsp/input.jsp ,輸入「早上好」,點擊提交,可獲得下面的結果 app