spring3.0-based Spring MVC project with XML configuration

Source: Internet
Author: User
Tags aop xmlns

Spring MVC Framework

The Spring framework provides a full-featured MVC module for building WEB applications. Using spring's pluggable MVC architecture, you can choose to use a built-in spring web framework or a WEB framework such as Struts. Through the policy interface, the Spring framework is highly configurable and contains a variety of view technologies, such as JavaServer Pages (JSP) technology, Velocity, Tiles, IText, and POI. The Spring MVC Framework does not know the views used, so you will not be forced to use only JSP technology. Spring MVC separates the roles of controllers, model objects, dispatchers, and handler objects, which makes them easier to customize. Common MVC Framework Comparisons

run performance on:

Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl, value stack.

development efficiency, basically just the opposite. It is worth emphasizing that spring MVC development efficiency is comparable to STRUTS2.

The reason for the low performance of STRUTS2 is due to OGNL and value stacks. So, if you have a high system concurrency, you can use Freemaker to display, instead of using OGNL and value stacks. In this way, there will be a considerable increase in performance.

spring3.0-based spring MVC project with XML configuration

Note: This project is all based on XML configuration. At the same time, integration of hibernate. Adopted is the development architecture of Spring mvc+hibernate+spring.

1. Building a Web project

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

3. The revised web.xml is 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 ">> <servlet> <servlet-name>dispatcherServlet< /servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet <
            /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> </init-param> <load-on-startup>1</load-on-startup>
        </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
 


4, add Web-config.xml (here contains related configuration for spring MVC)

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 "list" value= </bean> <!--Page view layer basic information set--> <bean id= "Viewresolver" class= "ORG.SPRINGFRAMEWORK.WEB.S" Ervlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" org.springframework  . Web.servlet.view.JstlView "/> <!--<property name=" prefix "value="/myjsp/"/>--> <property Name= "suffix" value= ". jsp"/> </bean> <!--servlet map list, all control layer controller servlet defined here--> <bean id= "urlmapping" class= "ORG.SP" Ringframework.web.servlet.handler.SimpleUrlHandlerMapping "> <property name=" Mappings "> &LT;PR ops> <prop key= "user.do" >userController</prop> </props> </prop erty> </bean> <bean id= "Usercontroller" class= "Com.sxt.action.UserController" > <property name= "use
 Rservice "ref=" UserService "></property> </bean> </beans>


5, under the Web-inf to increase the Service-config.xml (here contains service layer class 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 ">

	<bean id= "UserService" class= "Com.sxt.service.UserService" > <property name= "Userdao"
		Userdao "></property>
	</bean>
	
</beans>


6, add the Hib-config.xml under Web-inf (this contains the spring integration hibernate related configuration)

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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--> &LT;AOP: Aspectj-autoproxy/> <bean id= "DataSource" class=Asource "> <property name=" driverclassname "value=" Com.mysql.jdbc.Driver "> </property> <property name= "url" value= "Jdbc:mysql://localhost:3306/myhib" ></property&  
            Gt <property name= "username" value= "root" ></property> <property name= "password" value= "123456" &G t;</property> </bean> <bean id= "sessionfactory" class= "Org.springframework.orm.hibernate" 3.annotation. Annotationsessionfactorybean "> <property name=" DataSource "> <ref bean=" Datasourc E "/> </property> <property name=" Hibernateproperties "> <props&  
               	Gt The name of <!--key must be preceded by hibernate.   
                   --> <prop key= "Hibernate.dialect" > Org.hibernate.dialect.MySQLDialect </prop> &LT;PROp key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.hbm2ddl.auto" &GT;UPDATE&LT;/PR op> </props> </property> <property name= "Packagestoscan" > <value&gt ;com.sxt.po</value> </property> </bean> <bean id= "hibernatetemplate" class= "Org.springframewo" Rk.orm.hibernate3.HibernateTemplate "> <property name=" sessionfactory "ref=" Sessionfactory "></property" > </bean> <!--Configure a JdbcTemplate instance--> <bean id= "JdbcTemplate" Org.springframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" DataSource "/> </bean&  


Gt <!--configuration Transaction management--> <bean id= "Txmanager class=" Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" sessionfactory "></property> </bean> <tx: Annotation-driven transaction-manager= "Txmanager"/> <aop:config> &lT;aop:pointcut expression= "Execution (public * com.sxt.service.impl.*.* (..))" id= "Businessservice"/> &LT;AOP: Advisor advice-ref= "Txadvice" pointcut-ref= "Businessservice"/> </aop:config> <tx:advice id= "Txadvice" Transaction-manager= "Txmanager" > <tx:attributes> <tx:method name= "find*" true "read-only=" 
		The not_supported "/> <!--get Start method does not need to run in a transaction. In some cases there is no need to use transactions, such as obtaining data. Opening the transaction itself has a certain impact on performance--> <tx:method name= "*"/> <!--other methods run in practice--> </tx:attributes> </tx:advi
 Ce> </beans>


7, add Dao-config.xml under Web-inf (this contains the relevant configuration of 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"
	  Hibernatetemplate "></property>
	</bean>
</beans>


8, JavaBean class

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;
	}
}


9. Dao class

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
	}
	
}


10, Service category

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;
	}


11. Control class

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;
	}

	
}

OK, the configuration file is complete with spring MVC, but the project structure is very clear and experienced friends can read it.

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.