Spring MVC configuration Static Resources and resource bundles tutorial

Source: Internet
Author: User

1-Introduction of this tutorial article is based on:
    • Spring 4 MVC
2-Create a project
    • File/new/other.



Input:
    • Group ID:com.yiibai
    • Artifact Id:springmvcresource
    • Package:com.yiibai.springmvcresource


After the project is created as follows:


Don't worry about having an error message when the project is created. The reason is that we have not yet declared the Servlet library.

Attention:

Eclipse 4.4 (Luna) may have been wrong when creating a Maven project structure. Need to be repaired.
3-Configuring 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> &LT;ARTIFACTID&GT;JUNIT&LT;/ARTIFACTID&G            T <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> <versio N>3.1.0</version> <scope>provided</scope> </dependency> <!--Jst             L for JSP page-<!--Http://mvnrepository.com/artifact/javax.servlet/jstl-<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> & Lt;version>1.2</version> </dependency> <!--JSP API--<!--Http://mvnrepo Sitory.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.spring Framework</groupid> <artifactId>spring-core</artifactId> <version>4.1.4.rele Ase</version> </dependency> <!--http://mvnrepository.com/artifact/org.springframework/spri Ng-web-<dependency> <groupId>org.springframework</groupId> <artif          Actid>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> &LT;BUILD&G        T <finalname&Gt            Springmvcresource</finalname> <plugins> <!--config:maven Tomcat Plugin-- <!--Http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin--<plugin&gt                ; <groupId>org.apache.tomcat.maven</groupId> <artifactid>tomcat7-maven-plugin</artifacti D> <version>2.2</version> <!--Config:contextpath and P ORT (Default-/springmvcresource:8080)-<!--<configuration > <path>/</path> <port>8899</port> </co Nfiguration>-</plugin> </plugins> </build> </proje Ct>
4-Configure Spring

Configure Web. xml

Springcontextlistener will read the configuration file parameter 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-n Ame>spring-mvc</servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherSer Vlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet -mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> &L T;/servlet-mapping> <!--other XML Configuration--<!--Load by Spring Contextloaderlistener--& Lt;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>
To configure 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/conte Xt/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.springmvcre Source "/> <!--enables the Spring MVC Annotation Configuration--<context:annotation-config/&gt         ; <!--important!! -<!--Default SeRvlet Handler (for Resources *.css, *.js, Image,..)--<mvc:default-servlet-handler/> <mvc:annotati On-driven/> <!--Config Resource Mapping--<mvc:resources mapping= "/styles/**" location= "/web-in f/resources/css/"/> <!--Config Properties file--<bean id=" appproperties "class=" Org.sprin Gframework.beans.factory.config.PropertiesFactoryBean "> <property name=" Locations "> <list&gt     ;<value>classpath:applicationrb.properties</value></list> </property> </bean> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "P Refix "> <value>/WEB-INF/pages/</value> </property> <property name=" Suffi X "> <value>.jsp</value> </property> <property name=" Exposedconte        Xtbeannames ">    <list><value>appProperties</value></list> </property> </bean></beans& Gt
    • 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>
To configure static resources:
<!--/web-inf/spring-mvc-servlet.xml    --<!--Important!!-->   <!--Default servlet Handler (for Resources *.css, *.js, image,..) --   <mvc:default-servlet-handler/>   <mvc:annotation-driven/>  
Resource mapping <mvc:resources mapping ...;:
Configuration Properties File: 5-java class

    • Mycontroller.java
Package Com.yiibai.springmvcresource;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic 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, static resource and view resource Bundle (Properties file):
    • Applicationrb.properties
Text.loginprompt=enter user name and Passwordtext.username=user Nametext.password=password
Static resources
    • 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;}
Views (two JSP files)
    • staticresourcetest.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%> 
    • resourcebundletest.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%> 
7-Run the application first, you need to build the entire project before you run the application. Right-click the project and select:

Run Configuration:

Input:
    • Name:run Springmvcresource
    • Base directory: ${workspace_loc:/springmvcresource} to select "Browse workspace ..." To select the corresponding item.
    • Goals:tomcat7:run

Click Run
Static Resource testing:
    • Http://localhost:8080/SpringMVCResource/scripts/common.js
    • Http://localhost:8080/SpringMVCResource/styles/common.css
    • Http://localhost:8080/SpringMVCResource/staticResourceTest
Properties file test:
      • Http://localhost:8080/SpringMVCResource/resourceBundleTest

Spring MVC configuration Static Resources and resource bundles tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.