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

1.引入相关jar包

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

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

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

value="com.mysql.jdbc.Driver">

value="jdbc:mysql://localhost:3306/db_studentInfo">

true

true

org.hibernate.dialect.MySQLDialect

com.java1234.model.User

com.java1234.model.Grade

com.java1234.model.Student

3.配置web.xml文件

S2SHStudentInfoManage

index.jsp

StrutsPrepareAndExecuteFilter

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

StrutsPrepareAndExecuteFilter

/*

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

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 userList=(List)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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值