(二)spring MVC配置
2015-06-06 17:31
513 查看
使用Maven添加依赖的jar包
![](http://images0.cnblogs.com/blog2015/695982/201506/061726554737641.png)
第一个还没用上
刚开始没加spring-context,@Controller没法用
<description>Spring MVC Servlet</description>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>Spring MVC 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 代码如上,就不多解释了
还是说一点吧
注意:<load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动~
<url-pattern>/</url-pattern>,请求映射配置为“/”,框架能够捕获所有URL请求,同时又将静态资源的请求转交给web容器处理(之后将进一步说明)
2 <filter>
3 <description>
4 </description>
5 <display-name>CharacterEncodingFilter</display-name>
6 <filter-name>CharacterEncodingFilter</filter-name>
7 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
8 <init-param>
9 <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> 解决Post提交中文乱码问题,也不必自己写过滤器啦
1 <!-- xml schema -->
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
1 <!-- 自动扫描的包名 -->
<context:component-scan base-package="controller" />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
一开始没加也能用呢Q^Q,时好时坏报错 No mapping found for HTTP request with URI [xxx] in DispatcherServlet with name 'springMVC' 就查到缺这个了。为什么呢?
<mvc:annotation-driven />
(写到这里我已经心累了)
<!-- 对静态资源文件的访问 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
泪奔,添加他是因为忽然又报错,No mapping found for HTTP request with URI……
然而刚刚试着删掉还是能用呢,不能理解
1 <mvc:default-servlet-handler/>
2 <mvc:interceptors>
3 <mvc:interceptor>
4 <mvc:mapping path="/user/**" />
5 <bean class="interceptor.UserLoginInterceptor" />
6 </mvc:interceptor>
7 <mvc:interceptor>
8 <mvc:mapping path="/admin/**" />
9 <bean class="interceptor.AdminLoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
以上代码用来对未登录用户做权限管理,没啥好说的,由于只用到了预处理方法,也可以用过滤器来实现。但是拦截器和过滤器还是不一样的,感觉它的postHandle方法是过滤器做不到的。(没有用过,瞎猜的。
忘记了来源的图片
![](http://images0.cnblogs.com/blog2015/695982/201506/061726554737641.png)
第一个还没用上
刚开始没加spring-context,@Controller没法用
web.xml配置
1. 配置DispatcherServlet
<servlet><description>Spring MVC Servlet</description>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>Spring MVC 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 代码如上,就不多解释了
还是说一点吧
注意:<load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动~
<url-pattern>/</url-pattern>,请求映射配置为“/”,框架能够捕获所有URL请求,同时又将静态资源的请求转交给web容器处理(之后将进一步说明)
2. 过滤器
1 <!-- 过滤器 -->2 <filter>
3 <description>
4 </description>
5 <display-name>CharacterEncodingFilter</display-name>
6 <filter-name>CharacterEncodingFilter</filter-name>
7 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
8 <init-param>
9 <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> 解决Post提交中文乱码问题,也不必自己写过滤器啦
springMVC配置
1.xml schema配置
刚开始忽好忽坏的,忘记报啥错了……查了查是这个的问题,缺一不可呀1 <!-- xml schema -->
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
2.自动扫描包名
让其中的注解生效1 <!-- 自动扫描的包名 -->
<context:component-scan base-package="controller" />
3.视图解析器
JSP模板页面用到了JSTL标签库<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
4.自动注册
这个好用啦,会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean一开始没加也能用呢Q^Q,时好时坏报错 No mapping found for HTTP request with URI [xxx] in DispatcherServlet with name 'springMVC' 就查到缺这个了。为什么呢?
<mvc:annotation-driven />
(写到这里我已经心累了)
5.静态资源映射
本来可以介绍一番,但是截图上传图片太麻烦了(可能会上传PPT,可以参考<!-- 对静态资源文件的访问 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
6.静态资源处理?
也可以把静态资源转交给web容器处理。泪奔,添加他是因为忽然又报错,No mapping found for HTTP request with URI……
然而刚刚试着删掉还是能用呢,不能理解
1 <mvc:default-servlet-handler/>
7.拦截器
1 <!-- 拦截器 -->2 <mvc:interceptors>
3 <mvc:interceptor>
4 <mvc:mapping path="/user/**" />
5 <bean class="interceptor.UserLoginInterceptor" />
6 </mvc:interceptor>
7 <mvc:interceptor>
8 <mvc:mapping path="/admin/**" />
9 <bean class="interceptor.AdminLoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
以上代码用来对未登录用户做权限管理,没啥好说的,由于只用到了预处理方法,也可以用过滤器来实现。但是拦截器和过滤器还是不一样的,感觉它的postHandle方法是过滤器做不到的。(没有用过,瞎猜的。
忘记了来源的图片
![](http://images0.cnblogs.com/blog2015/695982/201506/062025362382729.png)
相关文章推荐
- 最佳新秀SSH十六Struts2它是如何工作的内部
- Java POI导入Excel文件
- Spring @EnableWebMvc
- springMVC bootstarp 用户权限基础框架+即时通讯
- Java读取属性文件简洁工具类
- Eclipse快捷键
- java 生成单号
- NetBeans 下使用EJB的例子
- JVM、Java编译器和Java解释器
- Eclipse上传项目到git.oschina
- Spring事务配置的五种方式
- 使用JSmooth制造java jar文件可以运行exe文件教程图像
- Java设计模式之单例与双重锁定
- 算法编程(JAVA)--八皇后问题
- java学习【课后笔记】
- 【Thinking in Java笔记】Java环境变量CLASSPATH
- leetcode--Rotate Image
- 探讨深入Java虚拟机之内存优化
- Ubuntu java 环境变量
- java PO BO VO DTO POJO DAO概念及其作用(附转换图)