SpringMVC(1)搭建及配置
<!-- web.xml -->
<!--中文--> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 前端控制器的配置 --> <servlet> <servlet-name>mvc-spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置mvc-spring-servlet.xml默认地址 classpath=src--> <!--<init-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>classpath:mvc-spring-servlet.xml</param-value>--> <!--</init-param>--> <!--表示启动容器时初始化该Servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
mvc-spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--注册controller--> <!-- 1.组件扫描--> <context:component-scan base-package="com.kaibing.controller"/> <!-- 2.控制器Handler:对于注解控制器可以单个配置,实际开发,使用组件扫描的形式 --> <!--<bean class="com.kaibing.controller.FirstController"></bean>--> <!--开启注释--> <!-- 第一种形式:配置注解式处理器映射器以及注解式处理器适配器 --> <!-- 注解式的HandlerMapping --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> --> <!-- 注解式的HandlerAdapter --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> --> <!-- 第二种形式:配置注解式处理器映射器以及注解式处理器适配器 通常情况下使用 : 使用下面的mvc注解驱动可以代替上边的 控制器映射器与控制器适配器。 mvc注解驱动默认加载很多的参数绑定方法,比如json转换解析器就默认加载。 --> <mvc:annotation-driven/> <!--ViewResolver 视图解析器--> <!--用于支持Servlet、JSP视图解析--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--需要加载 jstl 包--> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
Controller
@Controller @RequestMapping("/demo") public class FirstController { @RequestMapping("/hello") public String hello(Model model){ model.addAttribute("username","kaibing"); return "hello"; } //@RequestMapping(value={"/hello","user/create"}) //多个 // 最长匹配优先。 //@RequestMapping(value="/users/**") //@RequestMapping(value="/product?") //@RequestMapping(value="/product*") //@RequestMapping(value="/product/*") // @RequestMapping(value="/products/**/{productId}") @RequestMapping("/hello/{userId}") public String getUserId(Model model,@PathVariable(value = "userId") int userId){ model.addAttribute("username",userId); return "hello"; } }