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 the spring pluggable MVC architecture, you can choose to use a built-in spring web framework or a WEB framework such as Struts. With the policy interface, the Spring framework is highly configurable and includes a variety of view technologies, such as JavaServer Pages (JSP) technology, Velocity, Tiles, IText, and POI. The Spring MVC framework does not know which view to use, so it does not force you to use only JSP technology. Spring MVC separates the roles of controllers, model objects, dispatchers, and handler objects, and this separation makes them easier to customize. 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.

spring3.0-based spring MVC project with XML configuration

Note: This project is all based on XML configuration. At the same time, Hibernate is integrated. Using: Spring Mvc+hibernate+spring's development architecture.

1. Build a Web Project

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

3. Modify the Web. Xml 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 (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.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 mapping list, the servlet for all control layer controllers is 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. Add Service-config.xml under Web-inf (this includes the configuration of 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--&LT;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&  
            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 <!--Key's name is 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" class= " 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*" read-only= "true" propagation= " 
		Not_supported "/> <!--get Start method 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> </tx:advi
 Ce> </beans>


7. Add Dao-config.xml under Web-inf (this includes the related 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" ref= " 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 class

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

	
}

Well, the profile-based spring MVC, even though it's done, has a lot of configuration files, but the project structure is clear and experienced friends can understand 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.