日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC之访问静态文件

發布時間:2024/9/20 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC之访问静态文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在進行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之访问静态文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。