javascript代码实例教程-SpringMVC静态文件(图片)访问+js访问 简单小例子

发布时间:2019-04-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-SpringMVC静态文件(图片)访问+js访问 简单小例子脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

项目文件布局:

 

 

javascript代码实例教程-SpringMVC静态文件(图片)访问+js访问 简单小例子

web.XMl文件:

<?xML version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="https://java.sun.COM/xml/ns/javaee"
 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="https://java.sun.com/xml/ns/javaee
 https://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/spring-servlet.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>


spring-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="https://www.springframework.org/schema/beans"
 xmlns:context="https://www.springframework.org/schema/context"
 xmlns:p="https://www.springframework.org/schema/p"
 xmlns:mvc="https://www.springframework.org/schema/mvc"
 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="https://www.springframework.org/schema/beans
                    https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    https://www.springframework.org/schema/context
                    https://www.springframework.org/schema/context/spring-context-3.0.xsd
                    https://www.springframework.org/schema/mvc
                    https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    ">
    <;mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <bean name="/test/static" class="com.mcm.web.controller.StaticController">
     <proPErty name="methodNameResolver">
      <ref bean="paramMethodResolver"/>
     </property>
    </bean>
  <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  <property name="paramName" value="action"/>
    </bean>
 <!-- 配置视图解析器 -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/"/>
     <property name="suffix" value=".jsp"/>
 </bean>                 
 </beans>                 

StaticController.java文件:

package com.mcm.web.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 StaticController extends MultiActionController {
 public ModelAndView img(HttpServletRequest request,HttpServletResponse response){
  return new ModelAndView("/aaa");
 }
}

aaa.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="https://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 &#39;aaa.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">
 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
  </head>
 
  <body>
   <h1>静态文件访问</h1>
    <img "图片" src="img/pig.jpg">

  </body>
  <script type="text/javascript">
   $(document).ready(function(){
    alert("ready");
   });
  </script>
</html>


 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-SpringMVC静态文件(图片)访问+js访问 简单小例子全部内容,希望文章能够帮你解决javascript代码实例教程-SpringMVC静态文件(图片)访问+js访问 简单小例子所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。