为了方便大家,同时也为了加深自己的记忆,这里小落自己搭建一个ssh项目
我把项目搭建步骤都先写出来,使大家在编写代码上更明确 项目源码下载
1.新建一个项目 导入必须的jar包
2.创建db.properties文件并配置
db.properties
jdbc.user=root
jdbc.pwd=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.jdbcurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
3.创建hibernate.cfg.xml并配置
hibernate.cfg.xml
org.hibernate.dialect.MySQLDialect
false
false
org.springframework.orm.hibernate4.SpringSessionContext
update
applicationContext.xml
com.jsx.pojo
(附完整的applicationContext.xml)
com.jsx.pojo
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
struts
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts
/*
index.jsp
com.jsx.pojo
com.jsx.dao
com.jsx.service
com.jsx.action
10.在pojo层创建User实体 以及对应的User.hbm.xml映射文件
User.java
package com.jsx.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDao.java
package com.jsx.dao;
import com.jsx.pojo.User;
public interface UserDao {
public boolean register(User user);
public User login(User user);
}
UserDaoImpl.java
package com.jsx.dao;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import com.jsx.pojo.User;
public class UserDaoImpl implements UserDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public boolean register(User user) {
try {
sessionFactory.getCurrentSession().save(user);
return true;
} catch (HibernateException e) {
return false;
}
}
public User login(User user) {
User u = null;
u = (User)sessionFactory.getCurrentSession().createQuery("from User where username=:username and password=:password")
.setString("username", user.getUsername())
.setString("password", user.getPassword()).uniqueResult();
return u;
}
}
UserService.java
package com.jsx.service;
import com.jsx.pojo.User;
public interface UserService {
public boolean register(User user);
public User login(User user);
}
package com.jsx.service;
import com.jsx.dao.UserDao;
import com.jsx.pojo.User;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean register(User user) {
return userDao.register(user);
}
public User login(User user) {
return userDao.login(user);
}
}
2.实现ModelDriven
3.添加UserService的set注入
4.实现UserAction的对应方法UserAction.java
package com.jsx.action;
import com.jsx.pojo.User;
import com.jsx.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven{
private User user = new User();
public User getModel() {
return user;
}
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String register(){
boolean register = userService.register(user);
if(register){
return SUCCESS;
}else{
return ERROR;
}
}
public String login(){
User u = null;
u = userService.login(user);
if(u!=null){
ActionContext.getContext().getSession().put("LoginUser", u.getUsername());
return SUCCESS;
}else{
return ERROR;
}
}
}
struts.xml
struts-user.xml
/success.jsp
/error.jsp
15.创建applicationContext-beans.xml并配置bean
applicationContext-beans.xml
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
My JSP 'index.jsp' starting page
用户登录
用户注册
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
My JSP 'index.jsp' starting page
成功 ---${LoginUser}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
My JSP 'index.jsp' starting page
失败