springmvc注解基本入门
简单介绍使用springmvc注解的基本流程.
1.在web.xml中配置DispatcherServlet
<?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="MyWebApp" version="2.5"> <display-name>springMVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <!--在web.xml(web程序的入口) 配置了DispatcherServlet 并且指定所有以.do结尾的请求全部交给DispatcherServlet来处理--> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--配置/*会拦截所有请求,包括静态页面 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2.创建核心配置文件springmvc-servlet.xml(WEB-INF/...)
在给配置文件中,主要配置注解驱动,配置包扫描就不在配置Bean了(扫描注解),配置视图解析器
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解驱动,就不要配置HandlerMapping和HandlerAdapter了 --> <mvc:annotation-driven/>
<!-- 不再配置Controller的Bean了,直接配置包扫描 --> <context:component-scan base-package="cn.cool.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
<!-- 定义文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设定默认编码 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean>
<!-- 配置自定义的拦截器 --> <mvc:interceptors> <!-- 方式1:直接在这里配置<bean> 对所有的Controller都拦截 --> <bean class="cn.controller.MyInterceptor1"/> <bean class="cn.controller.MyInterceptor2"></bean> </mvc:interceptors> </beans>
3.定义视图(这里创建一个hello.jsp文件,符合上面的视图规则,所以要放到WEB-INF/views/)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 可以用el表达式,直接获取模型中的数据 -->
<font color="red" size="5">${msg }</font>
</body>
</html>
4.配置handler
@RequestMapping("test") @Controller public class LoghinController { @RequestMapping("/show1") public ModelAndView show1(){ ModelAndView mv = new ModelAndView("hello"); mv.addObject("msg","第一个springmvc程序1"); return mv; } }}
5.在浏览器输入指定的地址,运行完成
WEB-INF/{servletName}-servlet.xml
今天要比昨天好