The configuration and use of Spring MVC

Source: Internet
Author: User

The configuration and use of Spring MVC

Note Warehouse: https://github.com/nnngu/LearningNotes

Jar packages required by Spring MVC

The version used by Spring MVC in the article is 3.2.18, and the jar package is required as follows:

spring-webmvcjstl 1.1.2aopalliance 1.0commons-logging 1.1.1spring-aopspring-beansspring-contextspring-corespring-expressionspring-web

Java projects built using Maven need to add the following dependencies in Pom.xml:

        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-webmvc</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Jstl</groupId>            <artifactId>Jstl</artifactId>            <version>1.1.2</version>        </dependency>        <dependency>            <groupId>Aopalliance</groupId>            <artifactId>Aopalliance</artifactId>            <version>1.0</version>        </dependency>        <dependency>            <groupId>Commons-logging</groupId>            <artifactId>Commons-logging</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-aop</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-beans</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-context</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-core</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-expression</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework</groupId>            <artifactId>Spring-web</artifactId>            <version>3.2.18.RELEASE</version>        </dependency>
Pre-preparation

1. com.nnngu.entity Create under the packageUser.java

The code is as follows:

Package com.nnngu.entity;import java.io.Serializable; Public classUserImplementsSerializable {Private Static Final LongSerialversionuid = 1L;PrivateString name;PrivateInteger age;PrivateString pwd; PublicStringGetName() {returnName } Public void SetName(String name) { This.name= name; } PublicIntegerGetage() {returnAge } Public void Setage(Integer Age) { This. Age= age; } PublicStringgetpwd() {returnPwd } Public void setpwd(String pwd) { This.pwd= pwd; }}

2. Create two JSP pages in the location shown

create.jspThe code is as follows:

<%--Created by IntelliJ.User:lijiaweidate:13/02/2018time:14:08To change this template use File | Settings | File Templates.--%><%@ pagelanguage="Java"ContentType="text/html; Charset=utf-8 "pageencoding="UTF-8" %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">http-equiv="Content-type"content="text/html; Charset=utf-8 "> <title> Create User </title>Action="Save"Method="POST"> <fieldset> <legend> Create user </legend> <p> <label> name: &LT;/LABEL&G T <inputtype="Text"ID="Name"name="Name"TabIndex="1"> </p> <p> <label> age:</label> <inputtype="Text"ID="Age"name="Age"TabIndex="2"> </p> <p> <label> password:</label> <inputtype="Text"ID="PWD"name="PWD"TabIndex="3"> </p> <pID="Buttons"> <inputID="Reset"type="Reset"TabIndex="4"value="Reset"> <inputID="Submit"type="Submit"TabIndex="5"value="Create"> </p> </fieldset></form></body>

detail.jspThe code is as follows:

<%--Created by IntelliJ.User:lijiaweidate:13/02/2018time:14:08To change this template use File | Settings | File Templates.--%><%@ pagelanguage="Java"ContentType="text/html; Charset=utf-8 "pageencoding="UTF-8" %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">http-equiv="Content-type"content="text/html; Charset=utf-8 "> <title> User Details </title>ID="Gloobal"> 
Configure Spring MVC

1. Configure the following in the Web. xml file:

<?xmlVersion= "1.0" encoding= "UTF-8"?><web-appxmlns="Http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1">    <filter>        <description>Character Set Filter</description>        <filter-name>Encodingfilter</filter-name>        <filter-class>Org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <description>Character Set encoding</description>            <param-name>Encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>Encodingfilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <servlet>        <servlet-name>Appservlet</servlet-name>        <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>Contextconfiglocation</param-name>            <param-value>Classpath:spring/springmvc-context.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>Appservlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

2. Create in the location shownspringmvc-context.xml

springmvc-context.xmlThe code is as follows:

<?xmlVersion= "1.0" encoding= "UTF-8"?><beans:beansxmlns:beans="Http://www.springframework.org/schema/beans"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns:context="Http://www.springframework.org/schema/context"xmlns:mvc="Http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xsi:schemalocation="Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdHttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ">    <!--scan all classes in the package to complete the function of bean creation and automatic dependency injection-    <context:component-scanbase-package="Com.nnngu"/>    <!--start the spring MVC-based annotation feature to add controller and method mappings to the container --    <mvc:annotation-driven/>    <!--This class for spring MVC View resolution --    <beans:beanid="Viewresolver"class="Org.springframework.web.servlet.view.InternalResourceViewResolver">        <beans:propertyname="prefix"value="/web-inf/pages/"/>        <beans:propertyname="suffix"value=". JSP"/>    </beans:bean></beans:beans>

3. Write Controller

Created in the location shown UserController.java , the code is as follows:

Package Com.nnngu;import Com.nnngu.entity.User;import Org.springframework.stereotype.Controller;import Org.springframework.ui.Model;import Org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;/*** User Management */@Controller Public classUsercontroller {@RequestMapping("") PublicStringCreate(Model model) {return "Create"; }@RequestMapping("/save") PublicStringSave(@ModelAttribute("Form"User user, model model) {//User: The Form object that the view layer passes to the control layer; Model: the object that the control layer returns to the view layerModel.AddAttribute("User", user);return "Detail"; }}
Test

Start Project, enter in browserlocalhost:8080

The test was successful.

The configuration and use of Spring MVC

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.