Spring MVC配置静态资源和资源包教程
2021-07-20 06:06
阅读:447
YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
标签:web app project func title red input one war load
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
4.0.0 com.yiibai SpringMVCResourcewar 0.0.1-SNAPSHOT SpringMVCResource Maven Webapp http://maven.apache.org junit junit3.8.1 test javax.servlet javax.servlet-api3.1.0 provided javax.servlet jstl1.2 javax.servlet.jsp jsp-api2.2 provided org.springframework spring-core4.1.4.RELEASE org.springframework spring-web4.1.4.RELEASE org.springframework spring-webmvc4.1.4.RELEASE SpringMVCResource org.apache.tomcat.maven tomcat7-maven-plugin2.2
4- 配置Spring
配置 web.xml
SpringContextListener 将读取配置文件参数 contextConfigLocation:

- WEB-INF/web.xml
Archetype Created Web Application spring-mvc org.springframework.web.servlet.DispatcherServlet 1 spring-mvc / contextConfigLocation /WEB-INF/root-context.xml org.springframework.web.context.ContextLoaderListener
配置Spring MVC:
- WEB-INF/spring-mvc-servlet.xml
classpath:ApplicationRB.properties /WEB-INF/pages/ .jsp
appProperties
- WEB-INF/root-context.xml
配置静态资源:
资源映射 :


配置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
Spring MVC Resource example Red textGreen text
- resourceBundleTest.jsp
Spring MVC Resource Bundle example ${appProperties[‘text.loginPrompt‘]}
${appProperties[‘text.userName‘]}
${appProperties[‘text.password‘]}
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配置静态资源和资源包教程
标签:web app project func title red input one war load
原文地址:https://www.cnblogs.com/borter/p/9519831.html
上一篇:生产者消费者模型 线程池
下一篇:输出一个数组的全排列
评论
亲,登录后才可以留言!