Spring3.0 rest应用 mapping不能访问static资源的问题

          spring3 mvc支持rest,丰富的注解,应用起来非常方便,对所有的请求当做rest来处理,这就会带来一个问题,

如果是静态资源,如js、css和图片,就会导致无法完成请求。


我们可以通过Spring3.0的新注解解决这个问题

<mvc:resources location="/resources/" mapping="/resources/**"/>


1、项目目录结构




2、web.xml文件内容

<?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">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>  
          <param-name>contextConfigLocation</param-name>  
          <param-value>
              /WEB-INF/applicationContext.xml,
            /WEB-INF/rest-servlet.xml</param-value>  
        </init-param>
        <load-on-startup>1</load-on-startup>  
    </servlet>
    
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/oauth/*</url-pattern>
  </servlet-mapping>
 
  <filter>
    <filter-name>LocalEncodingFilter</filter-name>
    <filter-class>com.ceosoftcenters.ispace.filter.SetCharacterEncodingFilter</filter-class>
    
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>false</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>LocalEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>




3、rest-servlet.xml文件内容

<?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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.ceosoftcenters.ispace.controller" />
    <!-- Enables the Spring MVC @Controller programming model -->  
    
    <!-- Enables the Spring MVC @Controller programming model -->  
    <mvc:annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->  
    <mvc:resources mapping="/resources/**" location="/resources/" />
  .
  .
  .
  .
  />




4、controller的内容

@Controller
@RequestMapping("/user")
public class UserController {

    private UserService userService;
    private UserHostService userHostService;
    
    @Resource
    private MarshallingView marshallingView;
    @Resource
    private MappingJacksonJsonView jsonView;

    public UserHostService getUserHostService() {
        return userHostService;
    }

    public void setUserHostService(UserHostService userHostService) {
        this.userHostService = userHostService;
    }

    public MappingJacksonJsonView getJsonView() {
        return jsonView;
    }

    public void setJsonView(MappingJacksonJsonView jsonView) {
        this.jsonView = jsonView;
    }

    public MarshallingView getMarshallingView() {
        return marshallingView;
    }

    public void setMarshallingView(MarshallingView marshallingView) {
        this.marshallingView = marshallingView;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)   
    public ModelAndView getUsers() {
        List<UserBean> list = userService.queryUser();
        HashMap<String, List<UserBean>> map = new HashMap<String, List<UserBean>>();
        map.put("userlist", list);
        ModelAndView mav = new ModelAndView("user/userList",map);
        return mav;
    }
}




5、页面的内容(部分内容)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="authz" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet"
    href="<c:url value="/resources/css/login.css"/>" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.js"/>"></script>
<script type="text/javascript" src="<c:url value="/resources/js/Login.js"/>"></script>
</head>

<body>
    <authz:authorize ifNotGranted="ROLE_USER,ROLE_ADMIN,ROLE_VIP">
        <form action="<c:url value="/login.do"/>" method="post" id="loginForm">
            <table cellspacing="0" cellpadding="0" width="783" align="center"
                border="0">
                <tbody>
                    <tr>
                        <td colspan="3"><img src="<c:url value="/resources/images/login/login_head.jpg"/>"
                            width="783" height="254" /></td>
                    </tr>

                    <tr>
                        <td width="282"><img src="<c:url value="/resources/images/login/login_left.jpg"/>"
                            width="282" height="156" /></td>



这样就可以访问static的资源了。。。。。。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值