springmvc 配置java包_Spring MVC配置静态资源和资源包教程

1- 介绍

这篇教程文章是基于:

Spring 4 MVC

2- 创建一个项目

File/New/Other..

fd20e2e97d8149fa7615ec76a6588ebd.png

b9f4fe98b666b78d2f14175313071b2d.png

dc8b3d3b7898bbbcd370f3579471c2b2.png

输入:

Group ID: com.yiibai

Artifact ID: SpringMVCResource

Package: com.yiibai.springmvcresource

6a79d90c7cfeb1a8ec545d2e2f101529.png

项目被创建以后如下:

09ea06c5169fc938f117e8f47079d9ef.png

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

注意:

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

0c6e9327bd2a71eda30e7f8f34b695d9.png

3- 配置Maven

pom.xml

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">

4.0.0

com.yiibai

SpringMVCResource

war

0.0.1-SNAPSHOT

SpringMVCResource Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet

jstl

1.2

javax.servlet.jsp

jsp-api

2.2

provided

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-web

4.1.4.RELEASE

org.springframework

spring-webmvc

4.1.4.RELEASE

SpringMVCResource

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

4- 配置Spring

配置 web.xml

SpringContextListener 将读取配置文件参数 contextConfigLocation:

63074c603a35e0520816ee723dfc292a.png

WEB-INF/web.xml

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">

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

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">

classpath:ApplicationRB.properties

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

/WEB-INF/pages/

.jsp

appProperties

WEB-INF/root-context.xml

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">

配置静态资源:

资源映射 :

abe973be345e2b6799e7fdaabdf7c2cf.png

配置Properties文件:

5- Java类

98fc4bfbb414aa35b5de72272169f32c.png

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):

54d82ac96d482bcc05a5adf3c20e993c.png

ApplicationRB.properties

text.loginPrompt=Enter user name and password

text.userName=User Name

text.password=Password

静态资源

f4eab849edd4c1d4b957e248af6fd5c5.png

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文件)

bda9b1a69f007819d0dd993ab7493e40.png

staticResourceTest.jsp

pageEncoding="UTF-8"%>

Spring MVC Resource example

href="${pageContext.request.contextPath}/styles/common.css">

Red text
Green text

value="Click me!">

resourceBundleTest.jsp

pageEncoding="UTF-8"%>

Spring MVC Resource Bundle example

${appProperties['text.loginPrompt']}

${appProperties['text.userName']}

${appProperties['text.password']}

7- 运行应用程序

首先,运行应用程序之前,您需要构建整个项目。

右键单击项目并选择:

d6824bd61196bc6d9e7e1ebd771e4768.png

bcbcd06749de620a3407c9651a2a367b.png

运行配置:

28f09897c889ce78c8b212c638d088d0.png

cbd111e57c964086461b40491607df58.png

输入:

Name: Run SpringMVCResource

Base directory: ${workspace_loc:/SpringMVCResource} 可选择“Browse Workspace..." 来选对应项目。

Goals: tomcat7:run

f1eb17c717366ce185399fdfd3489c1c.png

点击 Run

aa5e1d9cc25b3f4ca90d016c9726abf5.png

静态资源测试:

http://localhost:8080/SpringMVCResource/scripts/common.js

e23bf6a9822ca28e72bada520dd7e14c.png

http://localhost:8080/SpringMVCResource/styles/common.css

a95ea904cca26764c1a103133afb2db0.png

http://localhost:8080/SpringMVCResource/staticResourceTest

7e77a1a60901e77b28c15ef29f91c993.png

属性文件测试:

http://localhost:8080/SpringMVCResource/resourceBundleTest

51e58102a2c16631ce43998dde246a93.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值