1. Background information
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. 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.
2. Common MVC Framework Comparisons
Performance on the Run
JSP+SERVLET>STRUTS1>SPRINGMVC>STRUTS2+FREEMARKER>>STRUTS2,OGNL, value stack.
The development efficiency
Basically the opposite. It is worth emphasizing that SPRINGMVC 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.
3. Core Principles The call process is as follows:
4. Development Combat (Spring+springmvc+hibernate)
When we use the SPRINGMVC development project, we usually use annotations (and of course the configuration file), which can greatly improve our development efficiency. Implement zero Configuration. Here we use annotations to re-start a spring MVC configuration from scratch. The steps are as follows:
Build a Web project and build the package structure and related classes for the entire project. As shown in the following:
Import the relevant jar package as follows: (Download Spring-framework-3.0.0.release, other hibernate-related jars)
The source code is as follows:
Xml
<?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 "><!--Configure Spring Core servlet--<servlet> <servlet-nam E>springmvc</servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServ Let </servlet-class> <!--can customize the location and name of the Servlet.xml configuration file by default to the Web-inf directory with the name [<servlet-name>]-servle T.xml, such as springmvc-servlet.xml--> <init-param> <param-name>contextconfiglocation</param -name> <param-value>/WEB-INF/hib-config.xml,/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet><!--url-pattern configured to/, without text can cause other static files (JS,CSS, etc.) to be inaccessible. If it is *.do, it will not affect the static fileQ-<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern> *.do</url-pattern> </servlet-mapping></web-app>
Springmvc-servlet.xml
The name Springmvc-servlet is because the <servlet-name> tag in the Web. Xml above is SPRINGMVC (<servlet-name> springmvc </ servlet-name>), plus the "-servlet" suffix to form the Springmvc-servlet.xml file name, if you change to spring, the corresponding file name is Spring-servlet.xml.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:mvc= "H Ttp://www.springframework.org/schema/mvc "xmlns:context=" Http://www.springframework.org/schema/context "xmln S:util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schem A/context http://www.springframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.O RG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/s Chema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd > <!--scan all classes in the Web package to complete the bean creation and self- Dynamic Dependency Injection Function--<context: Component-scan base-package= "Com.tgb.web"/><!--support spring3.0 new MVC annotations--><mvc:annotation-driven/> <!--start the Spring MVC Annotation feature, complete the request and annotation pojo mapping--<bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> <!--resolution of the name of the model view, That is, add the <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" to the Model view name P:prefi x= "/web-inf/jsp/" p:suffix= ". jsp" > <!--If you use Jstl, configure the following properties--<property name= "Viewclass" value= "or G.springframework.web.servlet.view.jstlview "/> </bean></beans>
hib-config.xml ( Spring integrated hibernate configured)
<?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-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ><!--Start the package scan feature to register classes with annotations such as @controller, @Service, @repository, @Component, as spring beans- <context:component-scan base-package= "Com.tgb"/> <!--support AOP annotations--><aop:aspectj-autoproxy/>& Lt;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" ></p roperty> <property name= "password" value= "123456" ></property> </bean> <bean ID = "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <property name= "DataSource" > <ref bean= "DataSource"/> </property> <property name= "Hibernateproperties" > <props> <!--key in front of the name to add hibernate . --<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> <prop key= "HIbernate.show_sql ">true</prop> <prop key=" Hibernate.hbm2ddl.auto ">update</prop> </props> </property><property name= "Packagestoscan" ><value>com.tgb.po</val ue></property> </bean> <bean id= "hibernatetemplate" class= " Org.springframework.orm.hibernate3.HibernateTemplate "><property name=" sessionfactory "ref=" Sessionfactory " ></property></bean><!--Configuring a JdbcTemplate instance--<bean id= "JdbcTemplate" class= " Org.springframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" DataSource "/> </bean> ; <!--configuring 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> <aop:pointcut expression= "Execution ( Public* Com.tgb.service.impl.*.* (..)) " Id= "Businessservice"/> <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:advice& Gt </beans>
index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "iso-8859-1"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >User.java
Package Com.tgb.po;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import javax.persistence.Id; @Entitypublic class User {@Id @generatedvalue (strategy =generationtype.auto) Private int id;private string Uname;private string Pwd;public string Getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;} 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;}}
Userdao.java
Package Com.tgb.dao;import Javax.annotation.resource;import org.springframework.orm.hibernate3.HibernateTemplate; Import Org.springframework.stereotype.repository;import Com.tgb.po.User; @Repository ("Userdao") public class Userdao {@Resourceprivate 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;}}
Userservice.java
Package Com.tgb.service;import Javax.annotation.resource;import Org.springframework.stereotype.service;import Com.tgb.dao.userdao;import Com.tgb.po.User; @Service ("UserService") public class UserService {@Resourceprivate 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;}}
Usercontroller.java
/p>
Package Com.tgb.web;import Javax.annotation.resource;import Org.springframework.stereotype.controller;import Org.springframework.ui.modelmap;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.sessionattributes;import Com.tgb.po.user;import Com.tgb.service.UserService; @Controller ("Usercontroller") @RequestMapping ("/user.do") Public class Usercontroller {@Resourceprivate userservice userservice; @RequestMapping (params= "Method=reg") Public String Reg (String uname) {System.out.println ("hellocontroller.handlerequest ()"); Userservice.add (uname); return "index";} Public UserService Getuserservice () {return userservice;} public void Setuserservice (UserService userservice) {this.userservice = UserService;}}
To run the test:
Http://localhost:8080/SpringMVCTest/user.do?method=reg&uname=kobe
The Usercontroller's Reg method is called to insert the data contents into the database.
5. SummaryNow the mainstream web MVC framework, in addition to struts this main force, followed by spring MVC, so this is a programmer need to master the mainstream framework, the framework is more choice, to deal with the changing needs and business, the implementation of the program is more natural. However, to flexibly use spring MVC to deal with most web development, it is necessary to master its configuration and principles.
Spring MVC and struts are very similar in principle (both based on the MVC architecture) and have a servlet that controls page requests, and jumps to the page after processing.
Introduction to Spring MVC 3.0