之前一直学习和使用的都是SSM框架,最近公司使用的webwork,跟SSH相似,今天试着搭建一把SSH。
话不多说,开干!
1、首先创建项目,选择jdk、spring+struts2+hibernate。填写项目名,之后一路狂点。
2、惯例开始配置服务器tomcat。
3、选择file-project structure,选中加载的jar包起名lib。
4、在modules中加入配置好的tomcat依赖包和lib包。
5、配置artifacts。点击+选择Web Application:Exploaded--from modules,之后对Available Elements进行put into。
6、在tomcat中配置deployment,选中artifact,配置application context为/ ,build之后ok。
7、到此环境初步配置已经完成了。需要我们手动创建如下常规目录。
action目录进行访问处理;Dao目录进行数据交互,定义接口;DaoImpl实现具体执行方法;model层写实体类以及hbm.xml映射文件;service目录编写相关逻辑代码;还会有util等看具体需求。
7.1、其中model中的实体类与映射文件可以通过工具自动生成。前提需要配置databases,并且连接成功(这步省略)。
在model中添加JPA,之后左下角会出现Persistence,点击后出现项目右键选择generate persistence mapping,选择by database schema,进入下一步。
7.2、选中数据库以及生成路径、所要操作的表以及勾选separate XML即可。
PS: 这里说一下我在构建完后,发现配置文件中需要spring-web.jar包,然后手动添加了一下。
至此 项目搭建已经初步成型了,下面附上最重要的配置信息以及简单测试代码。
web.xml
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
struts2
*.action
struts2
*.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.jsp
struts.xml
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
struts2
*.action
struts2
*.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.jsp
[点击并拖拽以移动]
applicationContext.xml
org.hibernate.dialect.MySQLDialect
true
classpath:hibernate.cfg.xml
hibernate.cfg.xml (此文件主要配置mapping resource和class路径,也可直接配置在applicationContext.xml中)
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/css
true
true
update
org.hibernate.dialect.MySQLDialect
true
jta
User实体:
package com.css.user.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Objects;
@Entity
public class User implements Serializable {
private String id;
private String name;
private Integer age;
private Integer tell;
@Id
@Column(name = "id", nullable = false, length = 255)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = true, length = 255)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "age", nullable = true)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Basic
@Column(name = "tell", nullable = true)
public Integer getTell() {
return tell;
}
public void setTell(Integer tell) {
this.tell = tell;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(id, user.id) &&
Objects.equals(name, user.name) &&
Objects.equals(age, user.age) &&
Objects.equals(tell, user.tell);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, tell);
}
}
实体映射 User.hbm.xml:
核心Dao:baseDao:
package com.css.user.Dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class baseDao {
private SessionFactory sessionFactory;
private Session session;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession(){
return sessionFactory.openSession();
}
}
Dao接口:userListDao:
package com.css.user.Dao;
import java.util.List;
public interface userListDao {
public List findUserList();
}
Dao实现:userDaoImpl:
package com.css.user.DaoImpl;
import com.css.user.Dao.userListDao;
import com.css.user.model.User;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import com.css.user.Dao.baseDao;
@Transactional
@Repository("userListDao")
public class userDaoImpl extends baseDao implements userListDao {
@Override
public List findUserList() {
Session session=getSession();
Transaction ts=session.beginTransaction();
System.err.println("========in findUserList=====");
Query query=session.createQuery("from User");
System.err.println("==========query:"+query.toString()+"==========");
List list=query.list();
ts.commit();
session.close();
return list;
}
}
action访问控制:
getUser(实现简单的框架验证):
package com.css.user.action;
import com.css.user.model.User;
import com.opensymphony.xwork2.ModelDriven;
import org.hibernate.SQLQuery;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.omg.PortableInterceptor.ServerRequestInfo;
/*import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;*/
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.web.struts.ActionSupport;
import java.sql.SQLClientInfoException;
import java.util.List;
public class getUser extends HibernateDaoSupport implements ModelDriven { //extends ActionSupport HibernateDaoSupport
private User adminUser = new User();
public User getAdminUser() {
return adminUser;
}
public void setAdminUser(User adminUser) {
this.adminUser = adminUser;
}
public String execute() throws Exception{
System.err.println("=============================in method--getUser=======================");
System.out.println("===============:"+adminUser.getName());
System.out.println("===============:"+adminUser.getId());
if(adminUser.getName().equals(adminUser.getId())){
return "success";
}else{
return "error";
}
}
@Override
public User getModel() {
return adminUser;
}
}
findUser (实现User表的数据查询):
package com.css.user.action;
import com.css.user.DaoImpl.userDaoImpl;
import com.css.user.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.css.user.DaoImpl.userDaoImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.css.user.Dao.userDao;
public class findUser extends HibernateDaoSupport implements ModelDriven{ //extends ActionSupport HibernateDaoSupport
private userDaoImpl userDaoIm;
//当添加有参构造方法时必须手动添加无参构造
public findUser() {
}
public String findUser() throws Exception{
System.err.println("=======hql========");
List userList=(List) ActionContext.getContext().get("userList");
userList = userDaoIm.findUserList();
if(userList.size()!= 0){
return "success";
}else{
return "error";
}
}
@Override
public Object getModel() {
return null;
}
public userDaoImpl getUserDaoIm() {
return userDaoIm;
}
public void setUserDaoIm(userDaoImpl userDaoIm) {
this.userDaoIm = userDaoIm;
}
}
JSP页面:
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
list.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
成功
error.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
失败