Spring MVC project implemented based on spring3.0 annotations

Source: Internet
Author: User
Tags aop bind

When we use the SPRNG MVC development Project, we usually use annotations, which can greatly improve our development efficiency. Implement zero Configuration. Here we re-start a spring MVC configuration from scratch. The project was developed in a fully annotated manner.

The previous build Project import jar package is no longer described.

1, modify Web. XML, the file contents 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 "> <servlet> <servlet-name>springmvc</servlet-nam e> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-cla ss> <init-param> <param-name>contextConfigLocation</param-name> <p
        Aram-value>/web-inf/hib-config.xml,/web-inf/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet- Name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> &

 Lt;/web-app>

2, Springmvc-servlet.xml configuration content is 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:p= "http://www.springframework.org/schema/p" 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/con Text Http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--scan all classes in the Web package to complete the BE An create and auto-dependent injection feature-<context:component-scan base-package= "COM.SXT"/> <!--start the Spring MVC Annotation feature, complete the request and annotations POJ Mapping O-<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> & lt;! --The resolution of the name of the model view, that is, the model view name is added before and after <bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "p:suffix=". jsp "/> </beans>
 


3, 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-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>


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


5. DAO class

Package Com.sxt.dao;

Import Javax.annotation.Resource;

Import org.springframework.orm.hibernate3.HibernateTemplate;
Import org.springframework.stereotype.Repository;

Import Com.sxt.po.User;

@Repository ("Userdao") public
class Userdao {
	@Resource
	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;
	}
	
}


6. Service class

Package com.sxt.service;

Import Javax.annotation.Resource;

Import Org.springframework.stereotype.Service;

Import Com.sxt.dao.UserDao;
Import Com.sxt.po.User;

@Service ("UserService") public
class UserService {
	@Resource
	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;
	}
	
}


7. Control class

Package com.sxt.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.sxt.po.User;
Import Com.sxt.service.UserService;


@Controller ("Usercontroller")
@RequestMapping ("/user.do") Public     
class Usercontroller  {

	@ Resource
	private 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;
	}

	
}


8, Web-inf under the establishment of index.jsp. The contents of index.jsp are as follows:

<body>
   


Well, the Spring MVC project, based on the spring3.0 annotation implementation, has also been completed. A little less than the configuration XML, but for inexperienced friends, the structure is not very clear, the establishment of inexperienced friends first from the configuration of XML, and then learn the annotation method.


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.