Detailed explanation of SPRINGMVC configuration

Source: Internet
Author: User
Tags aop

Original address: http://www.cnblogs.com/superjt/p/3309255.html

One, spring MVC Environment Build: (Spring 2.5.6 + Hibernate 3.2.0)

1. Jar Package Introduction

Spring 2.5.6:spring.jar, Spring-webmvc.jar, Commons-logging.jar, Cglib-nodep-2.1_3.jar

Hibernate 3.6.8:hibernate3.jar, Hibernate-jpa-2.0-api-1.0.1.final.jar, Antlr-2.7.6.jar, commons-collections-3.1, Dom4j-1.6.1.jar, Javassist-3.12.0.ga.jar, Jta-1.1.jar, Slf4j-api-1.6.1.jar, Slf4j-nop-1.6.4.jar, the corresponding database driver Jar pack

SPRINGMVC is an MVC framework based on Dispatcherservlet, where each request was first accessed by Dispatcherservlet, Dispatcherservlet is responsible for forwarding each request requests to the corresponding Handler,handler processing to return the corresponding views and models (model), the returned views and models can not be specified, That is, you can return only model or return view only or do not return.

Dispatcherservlet is inherited from HttpServlet, since SPRINGMVC is based on Dispatcherservlet, then we first to configure the Dispatcherservlet, So that it can manage what we want it to manage. HttpServlet is declared in the Web.xml file.

<!--Spring MVC configuration--> <!--======================================--> <servlet> <servlet-name> Spring</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ Servlet-class> <!--You can customize the location and name of the Servlet.xml configuration file by default to the Web-inf directory, and the name is [<servlet-name>]-
        Servlet.xml, such as Spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value>&nbsp; Default </init-param>--> <load-on-startup>1</load-on-startup> </servlet> <servlet-m apping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> &LT;/SERVL Et-mapping> <!--spring configuration--> <!--======================================--> <listener> <l Istenerclass> Org.springframework.web.context.ContextLoaderListener </listener-class>
</listener> <!--Specifies the directory where the spring bean's configuration file resides. Default configuration--> <context-param> <param-name>contextConfigLocation</param-name> <param-in Web-inf directory Value>classpath:config/applicationcontext.xml</param-value> </context-param>

Spring-servlet.xml Configuration

The name Spring-servlet is because the Web.xml <servlet-name> tag in the above value is spring (<servlet-name>spring</servlet-name >), coupled with the "-servlet" suffix to form the Spring-servlet.xml file name, if changed to Springmvc, the corresponding file name is Springmvc-servlet.xml.

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xml Ns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" 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-3.0.xsd Http://www.springframework.org/schema/ao P http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://w Ww.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context <a href= "htt P://www.springframework.org/schema/context/spring-context-3.0.xsd ">http://www.springframework.org/schema/

    Context/spring-context-3.0.xsd</a> "> <!--Enable spring MVC annotations--> <context:annotation-config/> <!--set the jar package where the annotations are used--> &Lt;context:component-scan base-package= "Controller" ></context:component-scan> <!--complete the mapping of request and annotation Pojo-- > <bean class= "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <! --On the path to the page resolution. Prefix: prefix, suffix: suffix--> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" P: prefix= "/jsp/" p:suffix= ". jsp"/> </beans>

Dispatcherservlet uses special beans to process request requests and generate corresponding view returns.

With respect to the return of the view, controller is only responsible for sending back a value, and then exactly what view is returned, is controlled by the view parser, the common view parser in JSP is Internalresourceviewresovler, it will require a prefix and a suffix

In the view parser above, if controller returns BLOG/INDEX, then the view after parsing through the view parser is/jsp/blog/index.jsp.

The main is to talk about controller.

A class that uses @controller to mark all the controller

Package controller;

Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RequestParam; Import entity.

User; @Controller//Similar to Struts action public class TestController {@RequestMapping ("test/login.do")//Request URL address mapping, similar to struts Action-mapping public String Testlogin (@RequestParam (value= "username") string username, string password, httpservletre Quest request) {//@RequestParam refers to the parameters that must be contained in the requested URL address map (unless the property required=false)//@RequestParam can be abbreviated as: @RequestPara M ("username") if (!) Admin ". Equals (username) | | !" 
        Admin ". Equals (password)) {return" loginerror ";//Jump page path (default is forwarding), this path does not need to include the prefix and suffix configured in Spring-servlet configuration file}
    return "Loginsuccess";
        @RequestMapping ("/test/login2.do") Public Modelandview testLogin2 (string Username, string password, int age) { Request and response do not have to appear inmethod, if it is not used, the name of the//parameter can be removed from the page control, and the parameter type is automatically converted to the IF (!) Admin ". Equals (username) | | !" Admin ". Equals (password) | |
        Age < 5) {return new Modelandview ("Loginerror");///manually instantiate Modelandview complete jump page (forward), the effect is equivalent to the above method returns the string} Return to New Modelandview (New Redirectview ("...).  /index.jsp ")); Redirect to page//redirect There is also a simple writing//return new Modelandview ("redirect: ...
    /index.jsp "); The @RequestMapping ("/test/login3.do") public modelandview testLogin3 (user user) {//is also supporting the parameter as a Form object, similar to struts
        Actionform,user do not require any configuration, directly write can be String username = User.getusername ();
        String password = User.getpassword ();
        
        int age = User.getage (); if (!) Admin ". Equals (username) | | !" Admin ". Equals (password) | |
        Age < 5} {return new Modelandview ("Loginerror");
    Return to New Modelandview ("loginsuccess"); @Resource (name = "Loginservice")//Get the ID of the bean in Applicationcontext.xml as LoginservIce, and injected into private loginservice loginservice;  Equivalent to the spring traditional injection method of write get and set methods, the benefit is simple and neat, eliminating the need for code @RequestMapping ("/test/login4.do") public String testLogin4 (User
        User) {if (loginservice.login (user) = False) {return "Loginerror";
    Return to "loginsuccess"; }
}

The above 4 methods examples, is a controller contains different request URL, also can use a URL to access, through the URL parameter to differentiate access to different methods, the code is as follows:

Package controller;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping ("/test2/login.do")  //

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.