1- 介绍

这篇教程文章是基于:
  • Spring 4 MVC

2- 创建一个项目

  • File/New/Other..



输入:
  • Group ID: com.yiibai
  • Artifact ID: SpringMVCResource
  • Package: com.yiibai.springmvcresource
 


项目被创建以后如下:

 

不要担心有错误消息在项目被创建时。原因是,我们还没有声明 Servlet 库。

注意:

Eclipse 4.4(Luna)在创建 Maven 项目结构时可能是有错误的。需要修复。
 

3- 配置Maven

  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.yiibai</groupId>
<artifactId>SpringMVCResource</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCResource Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Servlet API -->
<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <!-- Jstl for jsp page -->
<!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</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> <!-- Spring dependencies -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies> <build>
<finalName>SpringMVCResource</finalName>
<plugins> <!-- Config: Maven Tomcat Plugin -->
<!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version> <!-- Config: contextPath and Port (Default - /SpringMVCResource : 8080) --> <!--
<configuration>
<path>/</path>
<port>8899</port>
</configuration>
-->
</plugin>
</plugins>
</build> </project>

4- 配置Spring

 

配置 web.xml

SpringContextListener 将读取配置文件参数 contextConfigLocation:
  • WEB-INF/web.xml
<web-app 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_3_0.xsd"
version="3.0"> <display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- Other XML Configuration -->
<!-- Load by Spring ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml
</param-value>
</context-param> <!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>
配置Spring MVC:
  • WEB-INF/spring-mvc-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- Package Scan -->
<context:component-scan base-package="com.yiibai.springmvcresource" /> <!-- Enables the Spring MVC Annotation Configuration -->
<context:annotation-config /> <!-- Important!! -->
<!-- Default Servlet Handler (For Resources *.css, *.js, image,..) -->
<mvc:default-servlet-handler />
<mvc:annotation-driven /> <!-- Config resource mapping -->
<mvc:resources mapping="/styles/**" location="/WEB-INF/resources/css/" /> <!-- Config Properties file -->
<bean id="appProperties" 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/pages/</value>
</property> <property name="suffix">
<value>.jsp</value>
</property> <property name="exposedContextBeanNames">
<list><value>appProperties</value></list>
</property>
</bean>
</beans>
  • WEB-INF/root-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Empty --> </beans>
配置静态资源:
<!-- /WEB-INF/spring-mvc-servlet.xml -->

   <!-- Important!! -->
<!-- Default Servlet Handler (For Resources *.css, *.js, image,..) -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />
资源映射 <mvc:resources mapping ... >:
 

配置Properties文件:

5- Java类

 
  • MyController.java
package com.yiibai.springmvcresource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController { @RequestMapping(value = "/staticResourceTest")
public String staticResource(Model model) {
return "staticResourceTest";
} @RequestMapping(value = "/resourceBundleTest")
public String resourceBundle(Model model) {
return "resourceBundleTest";
}
}

6- 资源包,静态资源和视图

Resource Bundle (Properties file):

 
  • ApplicationRB.properties
text.loginPrompt=Enter user name and password
text.userName=User Name
text.password=Password
静态资源
  • scripts/common.js
function sayHello()  {
alert("Hello from JavaScript");
}
  • /WEB-INF/resource/css/commons.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"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC Resource example</title> <script type="text/javascript"
src="${pageContext.request.contextPath}/scripts/common.js"></script> <link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/styles/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>
  • resourceBundleTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC Resource Bundle example</title>
</head>
<body> <h2>${appProperties['text.loginPrompt']}</h2> ${appProperties['text.userName']} <input type="text" name="userName"> <br>
${appProperties['text.password']} <input type="password" name="password"> <br> </body>
</html>

7- 运行应用程序

首先,运行应用程序之前,您需要构建整个项目。
右键单击项目并选择:

运行配置:


输入:
  • Name: Run SpringMVCResource
  • Base directory: ${workspace_loc:/SpringMVCResource} 可选择“Browse Workspace..." 来选对应项目。
  • Goals: tomcat7:run

点击 Run
静态资源测试:
  • http://localhost:8080/SpringMVCResource/scripts/common.js
  • http://localhost:8080/SpringMVCResource/styles/common.css
  • http://localhost:8080/SpringMVCResource/staticResourceTest
属性文件测试:
    • http://localhost:8080/SpringMVCResource/resourceBundleTest

Spring MVC配置静态资源和资源包教程的更多相关文章

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

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  2. Spring MVC配置静态资源的正常访问

    SpringMVC如果过滤器过滤范围配置了/或者/*,那么框架会过滤所有请求,包括自己写的请求和静态资源请求,这样静态资源就不能正常加载,包括js文件.css文件.图片资源访问的时候都会出现404页面 ...

  3. Spring MVC 处理静态资源文件

    摘要: 三个方案: 1.方案一:激活Tomcat的defaultServlet来处理静态文件 2.方案二: 在spring3.0.4以后版本提供了mvc:resources (需要配置annotati ...

  4. 【Spring学习笔记-MVC-14】Spring MVC对静态资源的访问

    作者:ssslinppp       参考链接: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html  http://www.cnb ...

  5. spring mvc对静态资源的访问

    如果我们的项目使用的是springmvc,在web.xml中会有一段这的配置. <servlet> <servlet-name>springMvc</servlet-na ...

  6. 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html

    概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...

  7. Spring mvc 增加静态资源配置后访问不了注解配置的controller

    spring mvc 增加静态资源访问配置. 例如: <!-- 静态资源映射 --> <mvc:resources location="/static/" map ...

  8. spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)

    spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器) 资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式 内部资源视图解析器: http:// ...

  9. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

随机推荐

  1. ClipboardJS复制粘贴插件的使用

    1.简单的纯JS复制粘贴(兼容性差,只能用textarea标签) var btn=document.getElementsByClassName("btn")[0]; //复制按钮 ...

  2. UNDO表空间损坏导致数据库无法OPEN

    在数据库undo表空间文件损坏.或者undo表空间文件缺失的情况下.无法打开数据库. 这两种情况都能够视为一种情况处理,解决方法一样. 场景:在23:10的时候新建一个undo表空间undotbs02 ...

  3. iOS:地图笔记

    地图笔记 01. CLLocation -------------------------------------------------------- CLLocationManager 定位管理者 ...

  4. iOS怎样找到自己的沙盒

    iOS怎样找到自己的沙盒 在ios开发我们会用到沙盒,因为自己对沙盒理解的不够,所以找不到沙盒文件在哪里,当然要知道路径了 比如我的路径 NSString* cachepath = [NSHomeDi ...

  5. ant Form 常用 api

    经过 Form.create 包装的组件将会自带 this.props.form 属性,this.props.form 提供的 API 如下: 注意:使用 getFieldsValue getFiel ...

  6. lodash random

    _.random([min=0], [max=1], [floating]) 产生一个包括 min 与 max 之间的数. 如果只提供一个参数返回一个0到提供数之间的数. 如果 floating 设为 ...

  7. C-类型转换(陷阱)

    getchar() 返回值为int类型 1.自动类型转换(运算符两边变量类型不同时) 1).两个变量类型自动转换成一样的类型(会根据参数类型自动转换, 而不是直接位转换), 且运算结果也是转换后的类型 ...

  8. Spring 集成hibernate时配置连接释放模式

    http://zmfkplj.iteye.com/blog/220822 程序出现一个奇怪的现象,用Quartz作业调度启动任务,运行一段时间后,任务会卡在一个查询接口处,久久不能运行完毕. 我本能的 ...

  9. Python进行数值计算

    1.计算积分 (1)计算定积分 from scipy import integrate #定义函数def half_circle(x): return (1-x**2)**0.5 pi_half, e ...

  10. window安装rabbitmq

    Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接可以下载安装最新的版本: 下载并安装Eralng OTP For Wi ...