dotnet Programmer's Java Crawl Pit Tour (iii) of Spring MVC Chapter One

Source: Internet
Author: User
Tags aop dotnet

Using the MAVEN structure to build SPRINGMVC projects, develop tools for idea

First, the construction of MAVEN projects, template for WebApp

Second, configure the SPRINGMVC configuration in the Pom file (Springmvc needs to be servlet, such as the local no servlet package, need to be introduced) as follows:

<!--https://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-WEBMVC-< Dependency><groupid>org.springframework</groupid><artifactid>spring-webmvc</artifactid ><version>4.3.9.RELEASE</version></dependency><!--https://  Mvnrepository.com/artifact/javax.servlet/servlet-api-<dependency><groupid>javax.servlet </groupid><artifactid>servlet-api</artifactid><version>2.5</version><scope >provided</scope></dependency>

Third, configuring the core servlet in Web. Contextconfiglocation, which represents the location of spring's configuration file, has three settings, and we're just saying one of the ways that I'm more fond of myself.

<servlet>    <servlet-name>springMvc</servlet-name>    <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</ param-value>    </init-param>    <load-on-startup>1</load-on-startup></servlet> <servlet-mapping>    <servlet-name>springMvc</servlet-name>    <url-pattern>/</ Url-pattern></servlet-mapping>

Iv. Configure the XML file (Spring.xml) in the Resources catalogue and open the scanner and path parser 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:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsd> <!--configuration Pack Scan-<context:component-scan base- Package= "Com.web"/> <!--Configuring the path resolver--<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/ web-inf/pages/"/> <property name=" suffix "value=". jsp "/> </bean></beans>

To create a controller type, set the annotation as follows:

@Controller/*indicates that this is a controller category*/@RequestMapping ("/hell")/*indicates the path of this controller*/ Public classHellorwordcontroller {@RequestMapping ("/world")/*represents an interview path for this method*/@ResponseBody/*indicates that the display will return to the Web page*/     PublicString Hell () {return"Hello World";/*generally represents the path name of the view, and the configuration of the front and rear able stitching into the full path of the view page*/    }}

Six, set Tomcat, activate. Now, to stop a basic SPRINGMVC project, directly activate the response path.

Vii. @RequestMapping Notes:

@RequestMapping ("/path"): Map please, map please refer to & please, deal with model figures, images and video parsers. In addition to the method of decoration, you can also repair the category.

Depending on the value, the path of the
By means of the method (example: @RequestMapping (value = "/world", method = Requestmethod.get))
According to the rules set the parameters, for example: @RequestMapping (value = "/world", method = Requestmethod.get,params = {"username", "age!=10"}) with the following:

URL path mapping can be used with wildcard characters

To parse the placeholder:

The REST full method request
First configure the filter in Web. xml
The filter will control all requests, if the request contains _method parameters
_method can be set to delete or put
The discard will be converted into a response type request based on the parameters.
Note: can only be converted to post please

<!--Configure the rest conversion configuration, switch to post please delete or put--><filter>    <filter-name>hiddenhttpmethodfilter</ Filter-name>    <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</ filter-class></filter><filter-mapping>    <filter-name>hiddenhttpmethodfilter </filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

Obtain the request

/**/@RequestMapping ("/requestparam")publictrue, DefaultValue = "Xiao Wang") String name,                           @RequestParam (int age ) {    return " Page ";}

Get access to the information you request

Cookie is the name of a cookie.

/**/@RequestMapping ("/cookie") public String cookievalue (@ Cookievalue ("cookie name"), String cookie) {    return "page";}

Viii. using Pojo to set the value of the image kidnapped

SPring MVC automatically matches the reference name and the Pojo name to the value of the image's padding. Supports level of connectivity. such as: Dept.depid, Dept.address.tel, etc., with the following:

Controller layer:

  

Page layer: The name of the input box in the form name is consistent with the name of the intrinsic definition of the 躰, if you are using a restfull style, you need to define the hidden control named _method name, and the value of the control is the request method that needs conversion.

Ix. APIs using the servlet's original voice as parameters

X. Dealing with model numbers (1)

  

Xi. dealing with model numbers (2) map

  

12.@SessionAttributes

If you want to have a common model in multiple requests, you can label a @sessionattribute,springmvc on the controller to temporarily store the dependencies in the model in the HttpSession.
@SessionAttribute In addition to being able to specify the nature of the name that needs to be placed in the conversation, you can also pass through the model-specific type, specifying that the model needs to be placed in the conversation.

13. @ModelAttributes

  

dotnet Programmer's Java Crawl Pit Tour (iii) of Spring MVC Chapter One

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.