SpringMVC之訪問靜態檔案

來源:互聯網
上載者:User

標籤:rds   vc開發   pattern   xml檔案   ping   request   5.x   fonts   class   

我們在進行springMVC開發時,必定會在jsp頁面引入js檔案、img檔案和css檔案。大多數人會將這些分類存放在WebRoot檔案下建立的檔案夾下面。同時,會在web.xml檔案中配置攔截所有請求。這樣就造成了頁面無法訪問到js、img和css檔案夾中的檔案了。

        在SpringMVC中可以利用 <mvc:resources location="/img/" mapping="/img/**"/>來訪問。從而解決了上述問題。

        下面是,我寫的一個demo。

        先看看其它檔案。

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>
  <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/*-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup> <!-- 啟動tomcat時啟動springMFC -->
  </servlet>
  <servlet-mapping>
   <servlet-name>SpringMVC</servlet-name>
   <url-pattern>/</url-pattern><!-- 攔截所有請求 -->
  </servlet-mapping>
</web-app>
先看顯示圖片的:img.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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="<%=basePath%>">
    
    <title>My JSP ‘hello.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="styles.css">
 -->

  </head>
  
  <body> 
    你好SpringMVC!!!<br/>
   <h4>顯示一張圖片</h4>
   <br/>
   <img alt="圖片" src="img/we.jpg">
    </div>
  </body>
</html>

在java檔案中:

package com.yx.controller;

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 img(HttpServletRequest request,HttpServletResponse response){
  System.out.println("-----img-------");
  return new ModelAndView("/img");
  
 }
 public ModelAndView js(HttpServletRequest request,HttpServletResponse response){
  System.out.println("-----js-------");
  return new ModelAndView("/testJS");
  
 }
}

以上檔案與前面講的十分的相似,改動不大。

下面是spring的設定檔:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 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.2.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

 <!-- 常規單class多方法bean -->
 <bean name="/test/multi" class="com.yx.controller.MultiController">
  <property name="methodNameResolver">
   <ref bean="paramMethodResolver"/>
  </property>
 </bean>
 <!-- 常規單class單方法bean -->
 <bean name="/test/hello" class="com.yx.controller.HelloSpringMVCController"></bean>
 
 
 <!-- 靜態資源訪問 -->
   <mvc:resources location="/img/" mapping="/img/**"/> 
   <mvc:resources location="/js/" mapping="/js/**"/>
   <!--以上兩句就是設定spring的攔截器不對img檔案夾與js檔案夾的檔案進行攔截-->
    
 <!-- 參數名稱解析 -->
 <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  <property name="paramName" value="action"></property>
 </bean>

 <!-- 視圖解析器 -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/jsp"></property> <!-- 首碼 -->
  <property name="suffix" value=".jsp"></property> <!-- 尾碼 -->
 </bean>
     
</beans> 

這樣子圖片就可以顯示出來了:

SpringMVC之訪問靜態檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.