SPRINGMVC Environment Construction---XML version and annotated version

Source: Internet
Author: User

I. Establishment of JAVAWEB project (based on IntelliJ 14.0.3 building)

1, build a Java project, create a new folder under the Project WebApp, and then create a new Web-inf folder under the folder;

2. Build the Web. xml file under the Web-inf folder, copy the basic document structure from the Tomcat installation path/conf/web.xml, and modify the corresponding code to utf-8;

3. Create JSP folder under Web-inf to store relevant JSP files (V in MVC);

4, in the Web-inf set up Lib folder, used to store the relevant jar package;

5. Build the Web. xml file under Web-inf.

The completed project catalog is as follows:

Two, spring configuration:

1. The package that needs to be imported is as follows:

2. Web. XML configuration:

1) Open the Web. xml file in the project and note that the schema constraint for the Web-app tag of the modified file is as follows:

<?xml version= "1.0" encoding= "iso-8859-1"? ><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 "></<web-app >

2) Add a spring servlet to Web. XML to allow the program to load this servlet when it is started, configured as follows:

<servlet>        <servlet-name>spring</servlet-name>        <servlet-class>            Org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <load-on-startup>1</ Load-on-startup></servlet>

Note:<load-on-startup> element tag container whether the servlet is loaded when it is started

1) Its value must be an integer indicating the order in which the servlet should be loaded;

2) A value of 0 or greater than 0 o'clock indicates that the container loads and initializes the servlet when the application is started;

3) When the value is less than 0 or unspecified, the container will not load until the servlet is selected;

4) The lower the value of a positive number, the higher the priority of the servlet, and the more the application starts to load;

5) At the same time, the container will be loaded in its own order of choice.

3) Configure Spring-url interception:

<servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</ Url-pattern></servlet-mapping>

Note:<url-pattern>/</url-pattern> indicates that all URL requests with suffix/are blocked by spring

4) Configure the character encoding filter:

    <filter>        <filter-name>encodingFilter</filter-name>        <filter-class> org.springframework.web.filter.characterencodingfilter</filter-class>        <init-param>            < param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param >        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true </param-value>        </init-param>    </filter>    <filter-mapping>        < filter-name>encodingfilter</filter-name>        <url-pattern>/</url-pattern>    </ Filter-mapping>

5) welcome-page Configuration:

  <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file> index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>  </ Welcome-file-list>

2. Spring-servlet.xml configuration (mainly URL mapping, page directory and controller configuration)

1) Configure the Spring-servlet.xml file under the Web-inf folder (the name depends on the <servlet-name> tag in the Web. xml file)

2) The annotated version is configured as follows:

<?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:co ntext= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <context:component-scan base-package=" cn.com        "> <context:include-filter type=" annotation "expression=" Org.springframework.stereotype.Controller "/> <context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/> </ context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean class= " Org.springframework.web.servlet.view.InternalResourceViewResoLver "> <property name=" prefix "value="/web-inf/jsp "/> <property name=" suffix "value=". JSP "/&gt    ; </bean></beans>

Description

a) <context:component-scan>: Configuring the Spring component Scan, base-package need to configure the package path for the project

b) <bean>: path resolution for the steering page. Prefix: prefix, suffix: suffix

3) The XML version is configured as follows:

<?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-2.5.xsd "> <bean class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf /jsp "/> <property name=" suffix "value=". jsp "/> </bean> <bean id=" urlmapping "class=" Org.s Pringframework.web.servlet.handler.SimpleUrlHandlerMapping "> <property name=" Mappings "> <pro ps> <prop key= "/index/" >IndexAction</prop> </props> </property&gt    ; </bean> <bean id= "indexaction" class= "cn.com.IndexAction" > <property name= "View" > & Lt;value>index</value> </property>    </bean></beans> 

Note: When a bean is configured with attributes (including bean-formatted attributes), the bean's corresponding Java file must define a property value that is the same as the name of the property, and the getter and setter methods that have this attribute value are required.

3, Controller code implementation:

1) Annotated version:

2) XML version:

4. index.jsp File Code:

<%string data = Request.getattribute ("Hello"). toString (); Response.getwriter (). Write (data);%>

Three, tomcat configuration:

Tomcat version: apache-tomcat-7.0.54

Place the WebApp folder under the project package into the Apache-tomcat-7.0.54\webapps\ directory of the Tomcat decompression package, as follows:

Note The JSP and Lib folders are also moved to the Web-inf folder directory

Ps:tomcat's Debug method:

Add Debug.bat under the Apache-tomcat-7.0.54\bin directory, as follows:

Four, the effect shows:

1) Annotated version:

2) XML version:

Reference text:

Http://www.cnblogs.com/shinejaie/p/5244258.html

Http://www.cnblogs.com/shinejaie/p/5248159.html

SPRINGMVC Environment Construction---XML version and annotated version

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.