Spring MVC Learning Note (i) pure XML configuration based on spring2.5

Source: Internet
Author: User
Tags aop xmlns

Written in front of the small tail actually this also has spring is to see the school's video basic're far off somebody else is a good grace this is to do a study notes or more

Common MVC Framework Comparisons

Performance on the run:
Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl, value stack.
The development efficiency, basically opposite. It is worth emphasizing that spring MVC development efficiency and struts2 are comparable.

The reason for the low performance of STRUTS2 is due to the OGNL and value stacks. So, if your system is high-volume, you can use Freemaker to display it instead of using OGNL and value stacks. In this way, there will be significant performance improvements.

This article builds a Web project based on the XML configuration integrated Hibernate spring mvc+hibernate+spring Development architecture

Import jar Package (Spring.jar, Spring-webmvc.jar, Commons-logging.jar. Other jar packages are hibernate-related jar packages)

3. Configure the Web. xml file in the Web-inf directory at the same time as the other layers here in the configuration file take sibling directory

<?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 htt P://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">> <servlet> <servlet-name>dispatcherservlet& Lt;/servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet &
            Lt;/servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/web-inf/hib-config.xml,/web-inf/web-config.xml,/web-inf/service-config.xml,/web-inf/ Dao-config.xml</param-value> <!--custom initialization of various profiles--</init-param> <load-on-startup>1 </load-on-startup> <!--automatically initialize configuration when project is turned on </servlet> <servlet-mapping> <servlet-n Ame>dispatcherservlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
 

4. Add Web-config.xml (this includes spring MVC related configuration)

<?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 "> <!--controller method call rule definition--<bean
        Id= "Paramethodresolver" class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >
    <property name= "ParamName" value= "action"/> <property name= "defaultmethodname" value= "list"/> </bean> <!--Page view layer basic information--<bean id= "Viewresolver" class= "Org.springframework.web . servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframewo Rk.web.servlet.view.JstlView "/> <!--<property name=" prefix "value="/myjsp/"/>--> <proper Ty name= "suffix" value= ". JSP"/> </bean> <!--servlet mapping list, the servlet for all control layer controllers is defined here--<bean id= "urlmapping" class= "Org.s Pringframework.web.servlet.handler.SimpleUrlHandlerMapping "> <property name=" Mappings "> <p rops> <prop key= "user.do" >userController</prop> </props> </pro perty> </bean> <bean id= "Usercontroller" class= "Com.sxt.action.UserController" > <property name=
 "UserService" ref= "UserService" ></property> </bean> </beans>

5. Add Service-config.xml under Web-inf (this includes the configuration of the service layer Class)

<?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 id= "UserService" class= "Com.sxt.service.UserService" >
        <property name= "Userdao" ref= " Userdao "></property>
    </bean>

</beans>

6. Add Hib-config.xml under Web-inf (this includes the spring integrated hibernate-related configuration)

<?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: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.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/
    Spring-context-2.5.xsd "> <context:component-scan base-package=" com.sxt "/> <!--support AOP annotations-- <aop:aspectj-autoproxy/> <bean id= "DataSource" class= "Org.apacHe.commons.dbcp.BasicDataSource "> <property name=" driverclassname "value=" Com.mysql. Jdbc. Driver "> </property> <property name=" url "value=" Jdbc:mysql://localhost:3306/myhib "></property> <property name=" username "value=" root "></property> <prop Erty name= "Password" value= "123456" ></property> </bean> <bean id= "Sessionfactory" CLA ss= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <property name= "Dataso Urce "> <ref bean=" dataSource "/> </property> <property name=" Hi Bernateproperties "> <props> <!--key must precede the name with Hibernate.   
                   --<prop key= "Hibernate.dialect" > Org.hibernate.dialect.MySQLDialect  
 </prop>                  <prop key= "Hibernate.show_sql" >true</prop> <prop key= "HIBERNATE.HBM2DD L.auto ">update</prop> </props> </property> <property name=" Packa Gestoscan "> <value>com.sxt.po</value> </property> </bean> <bean ID = "Hibernatetemplate" class= "org.springframework.orm.hibernate3.HibernateTemplate" > <property name= " Sessionfactory "ref=" sessionfactory "></property> </bean> <!--Configure a JdbcTemplate instance--<bean ID = "JdbcTemplate" class= "org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "Dataso Urce "/> </bean> <!--configuration Transaction Management-<bean id=" Txmanager "class=" org.springframework.orm.hibernate3.Hi Bernatetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "></property> </ Bean> <tx:annotation-driven Transaction-manager= "Txmanager"/> <aop:config> <aop:pointcut expression= "Execution (public * com.sxt.service.impl.) *.*(..))" Id= "Businessservice"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Businessservice"/> </aop:confi g> <tx:advice id= "Txadvice" transaction-manager= "Txmanager" > <tx:attributes> <tx:method 
        Name= "find*" read-only= "true" propagation= "not_supported"/> <!--get starts with a method that does not need to run in a transaction. In some cases, it is not necessary to use transactions, such as fetching data. Turning on the transaction itself has some impact on performance--<tx:method name= "*"/> <!--other ways to run in practice-</tx:attributes> &lt
 ;/tx:advice> </beans>

7. Add Dao-config.xml under Web-inf (this contains the relevant configuration of the DAO layer Class)

<?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 id= "Userdao" class= "Com.sxt.dao.UserDao" >
      <property name= "hibernatetemplate" ref= " Hibernatetemplate "></property>
    </bean>
</beans>

8. Create the relevant class and package structure, as shown in the figure below

9. The various codes are as follows:

Package Com.sxt.po;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;

Import Javax.persistence.Id;
    @Entity public class User {@Id @GeneratedValue (strategy=generationtype.auto) private int Id;
    Private String uname;
    public int getId () {return id;
    } public void setId (int id) {this.id = ID;
    } public String Getuname () {return uname;
    } public void Setuname (String uname) {this.uname = uname;

}} package Com.sxt.dao;

Import Org.springframework.orm.hibernate3.HibernateTemplate;

Import Com.sxt.po.User;

    public class Userdao {private Hibernatetemplate hibernatetemplate;
        public void Add (User u) {System.out.println ("Userdao.add ()");
    Hibernatetemplate.save (U);
    } public hibernatetemplate Gethibernatetemplate () {return hibernatetemplate; } public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {this.hibernatetemplate = hibernatetemplate;

}} package Com.sxt.service;
Import Com.sxt.dao.UserDao;

Import Com.sxt.po.User;

    public class UserService {private Userdao Userdao;
        public void Add (String uname) {System.out.println ("Userservice.add ()");
        User U = new user ();
        U.setuname (uname);
    Userdao.add (U);
    } public Userdao Getuserdao () {return userdao;
    } public void Setuserdao (Userdao userdao) {This.userdao = Userdao;

}} package com.sxt.action;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.ModelAndView;

Import Org.springframework.web.servlet.mvc.Controller;

Import Com.sxt.service.UserService;

    public class Usercontroller implements Controller {private UserService userservice; @Override public Modelandview HandleRequest (httpservletrequest req, HttpServletResponse reSP) throws Exception {System.out.println ("hellocontroller.handlerequest ()");
        Req.setattribute ("A", "AAAA"); 
        Userservice.add (Req.getparameter ("uname"));
    return new Modelandview ("index");
    } public UserService Getuserservice () {return userservice;
    } public void Setuserservice (UserService userservice) {this.userservice = UserService;
 }


}

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.