Spring MVC體系結構(五)
阿新 • • 發佈:2018-06-21
conf let dispatch classpath 名稱 val 針對 控制 情況
一、spring mvc框架搭建步驟
1、導入jar文件
spring-web-3.2.0.RELEASE
spring-webmvc-3.2.0.RELEASE
2、在web.xml中配置servlet和其他參數
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 在web.xml中配置前端控制器,DispatcherServlet是spring最和核心的類--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 通過過濾器設置字符編碼 --> <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> </web-app>
3、配置applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:component-scan base-package="com.action"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
4、創建controller--->>處理請求的控制器
package com.action; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.entity.User; @Controller public class UserAction { @RequestMapping("submit") public ModelAndView show(User user){ ModelAndView m = new ModelAndView("list"); m.addObject("uname",user.getUname()); m.addObject("upass",user.getUpass()); return m; } }
二、controller控制器中參數傳遞的幾種方式
1、可以通過已經創建的javabean註入
import com.entity.User; @Controller public class UserAction { @RequestMapping("submit") public ModelAndView show(User user){ ModelAndView m = new ModelAndView("list"); m.addObject("uname",user.getUname()); m.addObject("upass",user.getUpass()); return m; } }
2、對應名稱自動填充
@RequestMapping("submit") public ModelAndView show(String uname,String upass,HttpServletRequest request){ ModelAndView m = new ModelAndView("list"); m.addObject("uname",uname);//需要和前臺的name名稱一致 m.addObject("upass",upass);//需要和前臺的name名稱一致 return m; }
3、名稱不一致的情況需要添加註解描述
@RequestMapping("submit") /** * 針對前臺name的名稱與參數名稱不一致的情況下使用 * value 參數名 * required 是否必須 * defaultValue 如果為空,就當前默認值 */ public ModelAndView show(@RequestParam(value="uname",required=false,defaultValue="xxx") String uname,String upass){ ModelAndView m = new ModelAndView("list"); m.addObject("uname",uname);//需要和前臺的name名稱一致 m.addObject("upass",upass);//需要和前臺的name名稱一致 return m; }
Spring MVC體系結構(五)