回顧一下struts2,struts2框架有以下特色: |
struts.xml配置文件,必須以struts.xml命名,且放在src目錄下【配置】html 每次請求action時,都建立action實例【單例/多例】java action類一成不變的直接或間接繼續ActionSupport類【類層面】web action類中的業務控制方法老是相相似的簽名且無參【方法層面】spring action類中,接收參數要用成員變量和對應的set方法或set/get方法【成員變量層面】express
|
一、什麼是springmvc,它與spring有什麼關係spring-mvc
springmvc屬於spring框架的後續產品,用在基於MVC的表現層開發,相似於struts2框架mvc
二、初識springmvc工做流程app
大體流程:請求Action(/hello.action)→DispatcherServlet→handler→adapter→開發者自定義的Action類(HelloAction)→ModelAndView→View→DispatcherServlet→JSP頁面框架
三、springmvc快速入門jsp
步驟:
(1)引入jar包
(2)配置 web.xml
(3)編寫Action代碼,並進行URL映射配置
3.一、引入jar包
須要引入的jar包分紅三個部分:
spring-core
spring-web
spring-webmvc
spring-core | commons-logging-1.2.jar spring-beans-3.2.5.RELEASE.jar spring-context-3.2.5.RELEASE.jar spring-core-3.2.5.RELEASE.jar spring-expression-3.2.5.RELEASE.jar |
spring-web | spring-web-3.2.5.RELEASE.jar |
spring-webmvc | spring-webmvc-3.2.5.RELEASE.jar |
3.二、配置web.xml
在web.xml文中註冊springmvc框架的核心控制器
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 註冊springmvc框架核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
3.三、編寫Action類,並進行URL映射配置
(1)編寫Action類的代碼
HelloWorldAction.java
package com.rk.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorldAction implements Controller { public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "這是個人第一個SpringMVC應用"); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } }
(2)對HelloWorldAction進行URL映射配置
在WebRoot/WEB-INF/目錄下建立springmvc-servlet.xml配置文件,xml頭部信息與spring.xml相同
注意:該配置文件的命名規則:web.xml文件中配置的<servlet-name>的值-servlet.xml
<servlet-name>-servlet.xml
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean name="/helloworld.action" class="com.rk.web.action.HelloWorldAction"></bean> </beans>
注意:<bean>的name屬性中以「/」開始,不然訪問不到。
3.四、添加JSP頁面
在/WebRoot/下建立jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>My SpringMvc</title> </head> <body> success.jsp<br> ${message } </body> </html>
四、加載自定義目錄下的springmvc.xml配置文件
在默認狀況下:springmvc框架的配置文件必須叫<servlet-name>-servlet.xml,且必須放在WebRoot/WEB-INF/目錄下。
咱們能夠在web.xml文件中,爲DispatcherServlet配置一個初始化參數,讓它去咱們指定的目錄下加載springmvc-helloworld.xml配置文件。
(1)假如springmvc-helloworld.xml放置在src目錄下的com.rk.web.action包下
web.xml代碼以下:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 註冊springmvc框架核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/com/rk/web/action/springmvc-helloworld.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
其中,<param-value>也能夠寫成classpath:...
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/rk/web/action/springmvc-helloworld.xml</param-value> </init-param>
(2)若是springmvc-helloworld.xml配置文件放在src目錄下
web.xml代碼以下:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 註冊springmvc框架核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-helloworld.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>