zoukankan      html  css  js  c++  java
  • Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包

    本例映射:css目录:

    pom.xml

     <properties>
      	<spring.version>4.3.5.RELEASE</spring.version>
      </properties>
      
      <dependencies>
      
         <!-- spring模块库 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <!-- transaction事务 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        
        <!-- Servlet dependencies -->
        <!-- servlet(HttpServletRequest,HttpServletResponse) --> 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
         
        <!-- Jstl for jsp page -->
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
         
        <!-- JSP API -->
        <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
        <dependency>
             <groupId>javax.servlet.jsp</groupId>
             <artifactId>jsp-api</artifactId>
             <version>2.2</version>
             <scope>provided</scope>
         </dependency>
         
        
        
      </dependencies>
      
      <build>
        <finalName>gugua18</finalName>
        <pluginManagement>
        	<plugins>
        		<plugin>
        			<groupId>org.apache.maven.plugins</groupId>
        			<artifactId>maven-war-plugin</artifactId>
        			<version>2.6</version>
        		</plugin>
        	</plugins>
        </pluginManagement>
      </build>
    

      

    web.xml

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      
      
      <servlet>
      	<servlet-name>springmvc</servlet-name>
      	<servlet-class> 
      	org.springframework.web.servlet.DispatcherServlet
      	</servlet-class>
      	<load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
      	<servlet-name>springmvc</servlet-name>
      	<url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <listener>
      	<listener-class>
      		org.springframework.web.context.ContextLoaderListener
      	</listener-class>
      </listener>
      
    </web-app>
    

      

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://www.springframework.org/schema/mvc
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd
    					http://www.springframework.org/schema/context
    					http://www.springframework.org/schema/context/spring-context.xsd">
    
    </beans>
    

      

    springmvc-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://www.springframework.org/schema/mvc
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd
    					http://www.springframework.org/schema/context
    					http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <!-- 引入包 -->
    <context:component-scan base-package="springmvc"/>
    
    <!-- 自动装配 -->
    <context:annotation-config/>
    
    
    <!-- Important!! -->
    <!-- 配置静态资源: -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    
    <!-- Config resource mapping -->
    <!-- 资源映射  -->
    <mvc:resources location="/WEB-INF/resources/css/" mapping="/styles/**"></mvc:resources>
    
    
    <!-- Config Properties file -->
    <!-- 加载文件属性  -->
    <bean id="addProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    	<property name="locations">
    		<list><value>classpath:ApplicationRB.properties</value></list>
    	</property>
    </bean>
    
    <!-- 视图配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix">
    		<value>/WEB-INF/views/</value>
    	</property>
    	<property name="suffix">
    		<value>.jsp</value>
    	</property>
    	<!-- exposedContextBeanNames属性来限制能够暴露到页面上的spring bean的名称列表 -->
    	<property name="exposedContextBeanNames">
    		<list>
    			<value>addProperties</value>
    		</list>
    	</property>
    </bean>
    
    </beans>
    

      

    将styles/css/目录映射到/WEB-INF/resources/css目录下

    <mvc:resources location="/WEB-INF/resources/css/" mapping="/styles/**"></mvc:resources>
    

      

    ApplicationRB.properties

    text.loginPrompt=Enter user name and password
    text.userName=User Name
    text.password=Password
    

      

    然后新建common.js/common.css

    注意路径;

    webapp/scripts/common.js

    function sayHello()  {    
       alert("Hello from JavaScript");
    }
    

      

    webaapp/WEB-INF/resources/css/common.css

    .button {
       font-size: 20px;
       background: #ccc;
    }
     
    .red-text {
       color: red;
       font-size: 30px;
    }
     
    .green-text {
       color: green;
       font-size: 20px;
    }
    

      

    然后是jsp

    staticResourceTest.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>resource example</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/common.js"></script>
    <link src="${pageContext.request.contextPath}/styles/css/common.css">
    </head>
    <body>
    
    
    <div class="red-text">Red Text</div>
    <br>
    <div class="green-text">Green Text</div>
    <br>
    <input type="button" class="button" onclick="sayHello();" value="Click me">
    
    
    </body>
    </html>
    

      

    staticBoundleTest.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>boundle resouce example</title>
    </head>
    <body>
    
    <h3>${addProperties['text.loginPrompt']}</h3>
    
    
    ${addProperties['text.userName']}:<input type="text" name="userName"/><br>
    ${addProperties['text.password']}:<input type="password" name="password"/>
    
    </body>
    </html>
    

      

    控制器:

    MyController.jva

    package springmvc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class MyController {
    
    	
    	@RequestMapping(value="/staticResourceTest", method=RequestMethod.GET)
    	public String staticResource(Model model)
    	{
    		
    		return "staticResourceTest";
    	}
    	
    	@RequestMapping(value="/staticBoundleTest", method=RequestMethod.GET)
    	public String staticBoundle(Model model)
    	{
    		return "staticBoundleTest";
    	}
    }
    

      

    注意方法中一定要带:Model model,否则系统配置的文件属性,无法传递到jsp页面

    访问地址:

    http://localhost:8080/gugua18/staticBoundleTest

    http://localhost:8080/gugua18/staticResourceTest

  • 相关阅读:
    发布国内首个无服务器容器服务,运维效率从未如此高效
    阿里云异构计算团队亮相英伟达2018 GTC大会
    阿里如何将“高峰前扩容、高峰后缩容”的梦想照进现实?
    迁移到 GRUB 2
    (11计科1班-孙鹏启)SHELL脚本—期末成绩统计
    硬盘分区的c盘在外圈还是内圈
    Linux内核设计的艺术(第2版)
    卸载,弹出,安全移除驱动器 的区别
    (11级计科2班-张文旭)关于LINUX连接网络问题
    安装Zend Guard Loader
  • 原文地址:https://www.cnblogs.com/achengmu/p/9186567.html
Copyright © 2011-2022 走看看