S2SH Project Framework Construction (full annotation)

Source: Internet
Author: User

1. Introducing the relevant JAR package

2. Configure the spring configuration file named Applicationcontext.xml(configured and placed in the src directory)

<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:context= "Http://www.springframework.org/schema/context"
xmlns:p= "http://www.springframework.org/schema/p"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xmlns:ehcache= "Http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://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
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
Http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd ">


<bean id= "DataSource"
class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname"
Value= "Com.mysql.jdbc.Driver" >
</property>
<property name= "url"
Value= "Jdbc:mysql://localhost:3306/db_studentinfo" >
</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= "DataSource"/>
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.show_sql" >true</prop>
<prop key= "Hibernate.format_sql" >true</prop>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name= "Annotatedclasses" >
<list>
<value>com.java1234.model.User</value>
<value>com.java1234.model.Grade</value>
<value>com.java1234.model.Student</value>
</list>
</property>
</bean>

<!--annotations Support-
<context:annotation-config/>

<!--set up a class package that requires a spring annotation scan--
<context:component-scan base-package= "com.java1234"/>


<!--configuration Transaction Manager-
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>

<!--Configure transaction propagation characteristics--
<tx:advice id= "Transactionadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<tx:method name= "*save*" propagation= "REQUIRED"/>
<tx:method name= "*delete" propagation= "REQUIRED"/>
<tx:method name= "update*" propagation= "REQUIRED"/>
<tx:method name= "get*" read-only= "true"/>
<tx:method name= "load*" read-only= "true"/>
<tx:method name= "find*" read-only= "true"/>
<tx:method name= "*" read-only= "true"/>
</tx:attributes>
</tx:advice>

<!--configure which classes of which methods participate in the transaction--
<aop:config>
<aop:advisor pointcut= "Execution (* com.java1234.service.*.* (..))" advice-ref= "Transactionadvice"/>
</aop:config>


</beans>

3. Configuring the Web. xml file

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 ">
<display-name>S2SHStudentInfoManage</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Applicationcontext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

4. Example Daoimpl

Package Com.java1234.dao.impl;

Import java.util.List;

Import Javax.annotation.Resource;

Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Repository;

Import Com.java1234.dao.UserDao;
Import Com.java1234.model.User;

@Repository
public class Userdaoimpl implements userdao{

Private Sessionfactory sessionfactory;

@Override
Public user login (user user) throws Exception {
User Resultuser=null;
Session session=this.getsession ();
Query query=session.createquery ("from User u where u.username=?") and u.password=? ");
Query.setstring (0, User.getusername ());
Query.setstring (1, User.getpassword ());
@SuppressWarnings ("Unchecked")
List<user> userlist= (list<user>) query.list ();
if (Userlist.size () >0) {
Resultuser=userlist.get (0);
}
return resultuser;
}

@Resource
@Autowired
public void Setsessionfactory (Sessionfactory sessionfactory) {
This.sessionfactory = sessionfactory;
}

Public Session getsession () {
return Sessionfactory.getcurrentsession ();
}


}

5. Example Serviceimpl

Package Com.java1234.service.impl;

Import Javax.annotation.Resource;

Import Org.springframework.stereotype.Service;

Import Com.java1234.dao.UserDao;
Import Com.java1234.model.User;
Import Com.java1234.service.UserService;

@Service
public class Userserviceimpl implements userservice{

@Resource
Private Userdao Userdao;

@Override
Public user login (user user) throws Exception {
return Userdao.login (user);
}

}

6. Example action

Package com.java1234.action;

Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;

Import org.apache.struts2.convention.annotation.Action;
Import Org.apache.struts2.convention.annotation.Namespace;
Import Org.apache.struts2.convention.annotation.Result;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.springframework.context.annotation.Scope;

Import Com.java1234.model.User;
Import Com.java1234.service.UserService;
Import Com.java1234.util.StringUtil;
Import Com.opensymphony.xwork2.ActionSupport;

@Scope ("prototype")
@Namespace ("/")
@Action (value= "Login", results={@Result (name= "Success", type= "redirect", location= "/main.jsp"), @Result (name= " Error ", location="/index.jsp ")})
public class Loginaction extends Actionsupport implements servletrequestaware{

/**
*
*/
Private static final long serialversionuid = 1L;

@Resource
Private UserService UserService;

private user user;
Private String error;
Private String Imagecode;

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

Public String GetError () {
return error;
}

public void SetError (String error) {
This.error = error;
}

Public String Getimagecode () {
return imagecode;
}

public void Setimagecode (String imagecode) {
This.imagecode = Imagecode;
}


HttpServletRequest request;

@Override
Public String Execute () throws Exception {
Get session
HttpSession session=request.getsession ();
if (Stringutil.isempty (User.getusername ()) | | Stringutil.isempty (User.getpassword ())) {
error= "User name or password is empty! ";
return ERROR;
}
if (Stringutil.isempty (Imagecode)) {
Error= "Verification code is empty! ";
return ERROR;
}
if (!imagecode.equals (Session.getattribute ("SRand"))) {
Error= "Verification Code Error! ";
return ERROR;
}
try {
User Currentuser=userservice.login (user);
if (currentuser==null) {
error= "User name or password is wrong! ";
return ERROR;
}else{
Session.setattribute ("CurrentUser", CurrentUser);
return SUCCESS;
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return SUCCESS;
}

@Override
public void Setservletrequest (HttpServletRequest request) {
TODO auto-generated Method Stub
This.request=request;
}


}

S2SH Project Framework Construction (full annotation)

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.