springmvc的簡單工作流程與三種controller簡單描述
阿新 • • 發佈:2019-01-25
1、springmvc請求過程
2、開發springmvc的一般流程
1 )匯入開發需要的jar包
com.springsource.javax.servlet.jsp.jstl-1.1.2.jarcom.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar
3)建立contorller(控制器) 例子: 繼承AbstractController類並複寫handleRequestInternal()方法<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 配置springmvc的分發器servlet --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通過初始化引數指定配置檔案的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:action-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
public class HelloController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("HomeController-------" + req.getParameter("name")); //向request域中新增東西 req.setAttribute("msg", "hello " + req.getParameter("name")); return new ModelAndView("home","msg","hello " + req.getParameter("name"));//home為邏輯名 } }
4)建立spring配置檔案/WEB-INF/action-servlet.xml(此路徑為預設路徑) 配置檢視解析器 配置控制器
其中控制器會返回一個ModelAndView的物件,然後再經過DispatcherServlet呼叫ViewResolver檢視解析器進行解析(怎麼解析的呢? 字首名 + 返回的邏輯名 + 字尾名 解析完成後跳轉到相應的資源(一般為jsp頁面)) 例子:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- bean名url處理器對映:預設 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<!-- 優先順序 -->
<property name="order" value="3"></property>
</bean>
<!-- 簡單url處理器對映 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<map>
<entry key="/abc.action" value="HelloController"></entry>
<entry key="/save.action" value="HelloController"></entry>
</map>
</property>
<property name="order" value="2"></property>
</bean>
<!-- 控制器類名處理器對映 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="order" value="4"></property>
</bean>
<!-- 配置控制器HomeController -->
<bean id="homeController" name="/home.action" class="cn.springmvc.controller.HelloController"></bean>
<!-- 命令控制器 -->
<bean name="/mycommand.action" class="cn.springmvc.controller.HelloController"></bean>
<!-- 表單控制器 -->
<bean name="/myform.action" class="cn.itcast.springmvc.controller.MyFormController">
<!-- personForm為邏輯名 -->
<property name="formView" value="personForm"></property>
<!-- success為邏輯名 -->
<property name="successView" value="success"></property>
</bean>
<!-- 嚮導表單控制器 -->
<bean name="/mywizardform.action" class="cn.itcast.springmvc.controller.MyWizardFormController">
<!-- 配置表單頁面 -->
<property name="pages">
<list>
<value>/wizard/1</value>
<value>/wizard/2</value>
<value>/wizard/3</value>
</list>
</property>
</bean>
<!-- 檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 字首 /WEB-INF/jsps/home.jsp-->
<property name="prefix" value="/WEB-INF/jsps/"></property>
<!-- 字尾 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
首先,所有的請求都會經過DispatchserServlet處理,然後通過HandlerMapping(處理機對映)把具體的請求分發具體的controller(控制器)上,然後控制器執行預設方法,執行完後返回一個物件(也是就是ModelAndView類),然後ModelAndView將請求返回給DispatcherSevlet在把這個具體的工作交個一個專門的元件去處理結果,也就是ViewResolver(檢視解析器--框架提供)去解析,解析完成後生成一個view(一般為jsp頁面),最後將請求返回給客戶端瀏覽器
三種Controller。
CommandController(命令控制器)
需要繼承AbstractCommandController類,並重寫handle方法
* 通過構造方法註冊命令類和命令名稱,如:
public MyCommandController(){
//註冊命令類
this.setCommandClass(Person.class);
//命令名稱
this.setCommandName("person");
}
public class PersonCommandController extends AbstractCommandController {
public PersonCommandController(){
//註冊命令類
this.setCommandClass(Person.class);
this.setCommandName("person");
}
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Person p = (Person) command;
System.out.println(p.toString());
return new ModelAndView("index");
}
FormController(表單控制器) 封裝表單中的資料
需要繼承SimpleFormController類,並重寫doSubmitAction方法
* 通過構造方法註冊命令類和命令名稱,如:
public MyFormController(){
this.setCommandClass(Person.class);
this.setCommandName("person");
}
* 在spring配置檔案中對錶單控制器進行配置,如:
<!-- 表單控制器 -->
<bean id="myFormController" name="/form.action" class="cn.itcast.controller.MyFormController">
<property name="successView" value="success"/>
<property name="formView" value="personForm"/>
</bean>
public class PersonFormController extends SimpleFormController {
//註冊命令物件
public PersonFormController() {
this.setCommandClass(Person.class);
this.setCommandName("person");
}
protected void doSubmitAction(Object command) throws Exception {
Person p = (Person) command;
System.out.println(p.toString());
}
}
WizardFormController(嚮導表單控制器)
* 需要繼承AbstractWizardFormController類,並重寫processFinish方法
*通過構造方法註冊命令類和命令名稱,如:
public MyWizardFormController(){
this.setCommandClass(Person.class);
this.setCommandName("person");
}
* 在spring配置檔案中對嚮導表單控制器進行配置,如:
<!-- 嚮導表單控制器 -->
<bean name="/wizard.action" id="myWizardFormControlle" class="cn.itcast.controller.MyWizardFormController">
<property name="pages">
<list>
<value>test/1</value>
<value>test/2</value>
<value>test/3</value>
</list>
</property>
</bean>
public class MyWizardFormController extends AbstractWizardFormController {
public MyWizardFormController() {
this.setCommandClass(Person.class);
this.setCommandName("person");
}
//處理完成
protected ModelAndView processFinish(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Person p = (Person) command;
System.out.println(p.toString());
return new ModelAndView("index");
}
//處理取消
protected ModelAndView processCancel(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
return new ModelAndView("wizard/jPersonBaseInfo");
}
}
其中嚮導表單控制器用的一般不多,一般用在例如:上一頁,下一頁,之類的頁面中
如:
<form action="mywizardform.action" method="post">
<table>
<tr>
<td>age:</td>
<td><input type="text" name=“age”/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="_target1" value="上一步" />
<input type="submit" name="_cancel" value="取消" />
<input type="submit" name="_finish" value="完成" />
</td>
</tr>
</table>
</form>
三個控制器的相同點:都需要經過建構函式註冊命令類