SPRINGMVC Case 2----annotation implementation based on spring2.5

Source: Internet
Author: User

As in the previous article, first look at the project structure and the jar package



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 "><servlet><servlet-name>springmvc</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/ 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></web-app>


Springmvc-servlet.xml

<?

XML version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "HT Tp://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/context/HTTP Www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--'s really into all the classes in the Web package are scanned to complete the creation of the bean and self-reliance on the input function --<context:component-scan base-package= "COM.SXT" ></context:component-scan> <!-- Start the annotation function for SPRINGMVC, complete the map of the request and annotation Pojo--<bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> <!--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" > &LT;PR Operty Name= "sUffix "value=". jsp "></property> </bean></beans>


Hib-config.xml

<?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.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ SPRING-TX-2.5.XSDHTTP://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 "></context:component-scan> <!--support AOP annotations--><aop:aspectj-autoproxy></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/crud" ></property><property name= "username" Value= "root" ></property><property name= "password" value= "root" ></property></bean>< Bean id= "sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <property name= "DataSource" ><ref bean= "DataSource" ></ref></property><property name= " Hibernateproperties "><props><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.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= "DataSource" ></property></bean><!--Configure 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.sxt.service.impl.*.* (..)) " Id= "Businessservice"/><aop:advisor advice-ref= "Txadvice" pointcut-ref= "Businessservice"/&GT;&LT;/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 be executed in the transaction. In some cases, it is not necessary to use transactions.

For example, get data. Opening the transaction itself has a certain effect on the performance itself--><tx:method name= "*"/></tx:attributes></tx:advice></beans>


User class

Package Com.sxt.po;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import javax.persistence.Id; @Entitypublic class User {private int Id;p rivate String Name;p rivate String pwd; @Id @generatedvalue (strategy=generationtype.auto) public int getId () {return Id;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getpwd () {return pwd;} public void SetPwd (String pwd) {this.pwd = pwd;}}

Userdao class

Package Com.sxt.dao;import Javax.annotation.resource;import org.springframework.orm.hibernate3.HibernateTemplate; Import Org.springframework.stereotype.component;import Org.springframework.stereotype.repository;import Com.sxt.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 class

Package Com.sxt.service;import Javax.annotation.resource;import Org.springframework.stereotype.component;import Org.springframework.stereotype.service;import com.sxt.dao.userdao;import Com.sxt.po.User; @Service ("UserService") public class UserService {@Resourceprivate Userdao Userdao;p ublic void Add (String name) {System.out.println (" Userservice.add () "); User U = new user (); u.setname (name); Userdao.add (u);} Public Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;}}

Usercontroller
Package Com.sxt.action;import Javax.annotation.resource;import Org.springframework.web.bind.annotation.requestmapping;import com.sxt.service.userservice;@ Org.springframework.stereotype.Controller ("Usercontroller") @RequestMapping ("/user.do") public class usercontroller{@Resourceprivate userservice userservice; @RequestMapping (params= "Method=reg") Public String Reg ( String uname) {System.out.println ("Usercontroller.reg ()"); Userservice.add (uname); return "index";} Public UserService Getuserservice () {return userservice;} public void Setuserservice (UserService userservice) {this.userservice = UserService;}}
index,jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
index2.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
To perform a test:

Http://localhost:8080/springmvc02/user.do?method=reg&uname=wuhaixu


SPRINGMVC Case 2----annotation implementation based on spring2.5

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.