SpringMVC 多控制器,静态资源访问,配置文件位置更改
2013-11-21 18:46
686 查看
SpringMVC 多控制器,静态资源访问,配置文件位置更改
1.配置文件名(springMVC-servlet.xml)称,位置更改:
上一篇博客中看到 是将 springMVC-servlet.xml 放置在 WEB-INF/springMVC-servlet.xml 下面的。这样做会有很多弊端(eg:不便于查找...)
为了配置方便和便于管理,我们将配置文件 springMVC-servlet.xml 放置在 src/config 目录下面。并且改名为 spring-servlet.xml.
为此我们需要在 web.xml 中指定该配置文件的位置
在<servlet></servlet>中添加<init-param>属性可以在项目初始化的时候加载
2.静态资源访问:
由于我们设置了 过滤内容为 <url-pattern>/</url-pattern> 即是对所有资源文件进行过率。可是在实际中我们不许要对一些静态资源(eg:CSS,JQuery,image等内容进行过滤)
我们需要在 spring-servlet.xml 文件中进行设置
3.对于在同一个Conroller 中添加多个方法:
在spring-servlet.xml 文件中需要添加
multi.jsp 文件为
访问结果为
1.配置文件名(springMVC-servlet.xml)称,位置更改:
上一篇博客中看到 是将 springMVC-servlet.xml 放置在 WEB-INF/springMVC-servlet.xml 下面的。这样做会有很多弊端(eg:不便于查找...)
为了配置方便和便于管理,我们将配置文件 springMVC-servlet.xml 放置在 src/config 目录下面。并且改名为 spring-servlet.xml.
为此我们需要在 web.xml 中指定该配置文件的位置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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*:config/spring*.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> </web-app>
在<servlet></servlet>中添加<init-param>属性可以在项目初始化的时候加载
<init-param> <param-name>contextConfigLocation</param-name> <!-- 代码编译后存放的位置,spring* 表示以 spring 开头的所有文件 --> <param-value>classpath*:config/spring*.xml</param-value> </init-param>
package org.springframework.web.servlet; @SuppressWarnings("serial") public class DispatcherServlet extends FrameworkServlet { /** * Create a new {@code DispatcherServlet} that will create its own internal web * application context based on defaults and values provided through servlet * init-params. Typically used in Servlet 2.5 or earlier environments, where the only * option for servlet registration is through {@code web.xml} which requires the use * of a no-arg constructor. * <p>Calling {@link #setContextConfigLocation} (init-param 'contextConfigLocation') * will dictate which XML files will be loaded by the * {@linkplain #DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext} * <p>Calling {@link #setContextClass} (init-param 'contextClass') overrides the * default {@code XmlWebApplicationContext} and allows for specifying an alternative class, * such as {@code AnnotationConfigWebApplicationContext}. * <p>Calling {@link #setContextInitializerClasses} (init-param 'contextInitializerClasses') * indicates which {@code ApplicationContextInitializer} classes should be used to * further configure the internal application context prior to refresh(). * @see #DispatcherServlet(WebApplicationContext) */ public DispatcherServlet() { super(); } }
2.静态资源访问:
由于我们设置了 过滤内容为 <url-pattern>/</url-pattern> 即是对所有资源文件进行过率。可是在实际中我们不许要对一些静态资源(eg:CSS,JQuery,image等内容进行过滤)
<servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 过滤的内容 --> <url-pattern>/</url-pattern> </servlet-mapping>因此我们需要设置专门的方法来对一些资源不进行过滤。
我们需要在 spring-servlet.xml 文件中进行设置
<!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 --> <mvc:resources location="/js/*" mapping="/js/**" /> <mvc:resources location="/img/*" mapping="/img/*" />该步骤需要 添加 Spring 的 mvc 命名空间。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/jdbc http://www.springframework.org/schema/context/spring-jdbc-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/context/spring-cache-3.2.xsd"> <beans>
3.对于在同一个Conroller 中添加多个方法:
package com.aoeng.springmvc.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class MultiController extends MultiActionController { public ModelAndView add(HttpServletRequest request, HttpServletResponse response) { System.out.println("-------add multi--------------"); Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); return new ModelAndView("/multi","map",map); } public ModelAndView execute(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView("/multi"); } }
在spring-servlet.xml 文件中需要添加
<bean name="/test/multi" class="com.aoeng.springmvc.controller.MultiController"> <!-- methodNameResolver 为 MultiController 类的父类 MultiActionController 中的属性 --> <property name="methodNameResolver" ref="parameterMethodNameResolver" /> </bean> <bean name="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="do" /><!-- 次属性的值不改变,将会默认使用 action --> </bean>
multi.jsp 文件为
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(function(){ alert("hello"); }); </script> </head> <body> ${map }<br> <c:forEach items="${map }" var="val"> ${val.key } -------> ${val.value} <br /> </c:forEach> <img src="img/img.png"> </body>
访问结果为
相关文章推荐
- SpringMVC 多控制器,静态资源访问,配置文件位置更改
- Springmvc配置静态文件后,无法访问控制器
- SpringMVC对静态资源文件的访问(配置)
- SpringMVC对静态资源文件的访问(配置)
- springMVC处理静态资源——jquery文件配置正确访问不到404错误信息
- SpringMVC对静态资源文件的访问(配置)
- SpringMVC对静态资源文件的访问(配置)
- SpringMVC对静态资源文件的访问(配置)
- springMVC对静态资源的访问-配置
- springMVC访问静态文件的配置
- SpringMVC 部署项目静态资源文件访问问题
- SpringMVC(10):配置静态资源访问(前端页面的元素显示)及实例
- DJango 1.8 配置静态资源文件可访问
- SpringMVC在web.xml中配置DispatcherServlet拦截了静态资源访问
- springMVC访问静态资源:为什么图片/js/css等文件写在jsp中是404不能获取
- springmvc访问静态资源的springmvc.xml配置
- SpringMVC 配置静态资源的正常访问
- 关于springMVC在配置了json时 静态文件无法访问的问题
- SpringMVC 笔记——访问静态资源DispatcherServlet 配置问题
- 如何配置SpringMVC能够访问静态资源