首页 > 代码库 > SpringMVC02静态资源的访问
SpringMVC02静态资源的访问
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="http://www.mamicode.com/"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/statics/css/style.css"> <script type="text/javascript" src="http://www.mamicode.com/statics/js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ alert("哈哈哈!"); }) </script> </head> <body> 哈哈 <img src="http://www.mamicode.com/statics/images/cat.jpg" alt="没有图片"> </body> </html>
如果没有配置静态资源访问,那么页面上是没有任何效果的!因为我们配置的核心控制的url 为 /
所以这些静态的资源路径页会被拦截!两种方式解决,推荐使用后者
<?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> <!-- 设置静态文件访问 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <!--配置springmvc的核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置spring配置文件的位置 以及名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/hello1" value="http://www.mamicode.com/myController" /> <entry key="/hello2" value="http://www.mamicode.com/myController" /> <entry key="/hello3" value="http://www.mamicode.com/myController" /> </map> </property> <!-- <property name="mappings"> <props> <prop key="/hello1">myController</prop> <prop key="/hello2">myController</prop> <prop key="/hello3">myController</prop> </props> </property> --> </bean> <!-- 配置静态资源的访问 **代表子文件下面的所有文件 --> <mvc:resources location="/statics/" mapping="/statics/**" /> <!-- 处理器 --> <bean id="myController" class="cn.bdqn.controller.MyController" /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp/" /> <property name="suffix" value="http://www.mamicode.com/.jsp" /> </bean> </beans>
SpringMVC02静态资源的访问
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。