您的位置:首页 > 其它

S2SH项目框架搭建(完全注解)

2016-01-20 20:58 375 查看
1.引入相关jar包

2.配置Spring配置文件,命名为applicationContext.xml(配置好后放到src目录下)

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

<!-- 注解支持 -->
<context:annotation-config/>

<!-- 设置需要进行Spring注解扫描的类包 -->
<context:component-scan base-package="com.java1234" />

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务传播特性 -->
<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>

<!-- 配置哪些类的哪些方法参与事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com.java1234.service.*.*(..))" advice-ref="transactionAdvice" />
</aop:config>

</beans>

3.配置web.xml文件

<?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.示例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.示例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.示例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 {
// 获取Session
HttpSession session=request.getSession();
if(StringUtil.isEmpty(user.getUserName())||StringUtil.isEmpty(user.getPassword())){
error="用户名或密码为空!";
return ERROR;
}
if(StringUtil.isEmpty(imageCode)){
error="验证码为空!";
return ERROR;
}
if(!imageCode.equals(session.getAttribute("sRand"))){
error="验证码错误!";
return ERROR;
}
try {
User currentUser=userService.login(user);
if(currentUser==null){
error="用户名或密码错误!";
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: