Detailed Step configuration for SPRINGMVC

Source: Internet
Author: User

The use of SPRINGMVC can also replace the struts2, of course, just replace the function of the business distribution, struts2 some other features it is not, or to struts2 what is the use.
Below I use SPRINGMVC instead of struts2 to integrate hibernate to achieve a simple employee query function.
With Springmvc There are two configuration files that need to be configured, one is Applicationcontext.xml, the other is Web. XML, the transaction manager is configured in Applicationcontext.xml, and attribute injection. In Web. XML, to add a SPRINGMVC servlet registration and Mapping (Dispatcherservlet), this servlet is SPRINGMVC's core controller, dedicated to each request, Then, according to the corresponding parameters distributed to the corresponding business controller processing, the business controller after processing will return a string to the core controller, the core controller is then redirected from the string or forwarded to the corresponding page. A configuration file must also be built for the core controller in the form of a core controller servlet name-servlet.xml, such as Springmvc-servlet.xml. The configuration file is placed under Web-inf.
The contents of Applicationcontext.xml are 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:util= "Http://www.springframework.org/schema/util" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "http:/ /www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-2.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/cOntext/spring-context-2.5.xsd "> <bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.LocalS        Essionfactorybean "> <property name=" configlocation "value=" Classpath:hibernate.cfg.xml "> </property> </bean> <bean id= "Deptdao" class= "Com.dao.DeptDAO" > <property name= "ses Sionfactory "> <ref bean=" sessionfactory "/> </property> </bean> <bean id=" Empdao "class=" Com.dao.EmpDAO "> <property name=" sessionfactory "> <ref bean=" sessionfactory " /> </property> </bean> <bean id= "Empservice" class= "Com.service.EmpService" > < Property Name= "Deptdao" ref= "Deptdao" ></property> <property name= "Empdao" ref= "Empdao" ></property > </bean> <!--transaction Manager--<bean id= "TransactionManager" class= "Org.springframework.orm.hiber Nate3. HibernatetransactiOnmanager "> <property name=" sessionfactory "ref=" sessionfactory "></property> </bean> &lt ;!            --Transaction Properties--<tx:advice id= "Mytx" transaction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "*"/> </tx:attributes> </tx:advice> <!--weave-in <aop:confi g> <aop:advisor advice-ref= "Mytx" pointcut= "Execution (* com.service.*.* (..))" /> </aop:config> </beans>

The contents of the

 web.xml are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" 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_2_5.xsd "> <!--Specify a path to another configuration--<context-param> <param-name >contextConfigLocation</param-name> <param-value>/WEB-INF/classes/app*.xml</param-value> </context-param> <!--control the filter of the session switch--<filter> <filter-name>opensession</filter-name > <filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class > </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern> /*</url-pattern> </filter-mapping> <!--load Applicationcontext.xml--<listener> < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--SPRINGMVC Registration and mapping-<servlet> <servlet-name>springMVC</servlet-name> < Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> </servlet> < Servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern > </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config>< /web-app>

The contents of the

 springmvc-servlet.xml are 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:util= "Http://www.springframework.org/schema/util" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "http:/ /www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-2.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/cOntext/spring-context-2.5.xsd "> <!--Specify automatic scan path--<context:component-scan base-package=" Com.action "&  Gt;</context:component-scan></beans>

The Empaction class code is as follows:

Package Com.action;import Java.util.list;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Com.service.EmpService, @Controller @requestmapping ("/emp.do") public class Empaction {    @Autowired    private Empservice Empservice;        @RequestMapping (params= "P=getall") public    String getAll (httpservletrequest request,httpservletresponse Response) {        List list = Empservice.getallemp ();        Request.setattribute ("list", list);        return "/web-inf/view/show.jsp";    }        }

@Controller means that the method is a business controller, @RequestMapping ("/emp.do") refers to the request Emp.do the core controller will be distributed to the business controller to deal with;
@RequestMapping (params= "P=getall") refers to this method of invoking the business controller when the request parameter is P=getall, and "/web-inf/view/show.jsp" is returned to the core controller. The core controller is then forwarded to the WEB-INF/VIEW/SHOW.JSP page to display all employee information.


The difference between SPRINGMVC and struts2:
1. Mechanism: Spring MVC's entry is the servlet, and Struts2 is the filter, which results in a different mechanism.

2. Performance: Spring will be slightly faster than struts. Spring MVC is a method-based design, and Sturts is class-based, each time a request is made with an action, each action is injected into the attribute, and spring is based on a method that is finer grained, but be careful to hold the same data as the servlet. SPRING3 MVC is a method-level interception that, after intercepting a method, injects the request data into a parameter based on annotations, and in Spring3 MVC, a method corresponds to a request context. The STRUTS2 framework is a class-level intercept, creating an action every time a request is made, and then invoking the setter getter method to inject the data from the request; Struts2 actually deals with the request through the setter getter method. ; in Struts2, an Action object corresponds to a request context.

3. Parameter passing: Struts is a parameter that can be accepted with attributes when it is accepted, which means that the parameters are shared by multiple methods.

4. Design thinking: Struts more in line with OOP programming ideas, Spring is more cautious, extended on the servlet.

5. Intercepter implementation mechanism: Struts has its own interceptor mechanism, and spring MVC uses an independent AOP approach. This leads to the profile of struts is still larger than spring MVC, although the configuration of struts can inherit, so I think in terms of use, Spring MVC use more concise, development efficiency Spring MVC is really higher than struts2. Spring MVC is a method-level intercept, a method that corresponds to a request context, and a method that corresponds to a URL, so it is easy to implement a restful URL spring3 mvc from the schema itself. STRUTS2 is class-level interception, a class corresponds to a request context, and implementing a restful URL is laborious because a method of struts2 action can correspond to a URL, and its class properties are shared by all methods. It is also not possible to identify the method that it belongs to by annotations or other means. Spring3 MVC method is basically independent, the request response data, requests data through parameters, processing results through the MODELMAP to the framework method is not shared between the variables, and struts2 make is more chaotic, although the method is also independent, But all of its action variables are shared, which does not affect the program's operation, but it gives us code and trouble reading the program.

6. In addition, SPRING3 MVC validation is also a bright spot, support JSR303, processing AJAX Requests is more convenient, just a note @responsebody, and then directly return the response text.

Source: http://www.cnblogs.com/liuling/archive/2013/02/07/sdfasdf.html

Reference: http://blog.csdn.net/zuxianghaung/article/details/6649269

Detailed Step configuration for SPRINGMVC

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.