index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录页面
/s:textfield
/s:password
/s:submit
/s:form
struts.xml
firstpage.jsp
index.jsp
LogAction
public class LogAction extends ActionSupport{
private String name;
private String pwd;
public String getName() {
return name;
}
public String getPwd() {
return pwd;
}
public void setName(String name) {
this.name = name;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String execute() throws Exception {
Login();
return super.execute();
}
public String Login(){
ActionContext context = ActionContext.getContext();
HttpServletRequest request = ServletActionContext.getRequest();
String name = (String) request.getParameter("name");
String pwd = (String) request.getParameter("pwd");
if (name.isEmpty() || pwd.isEmpty()){
return ERROR;
}else {
Usee login = UseeDao.login(name, pwd);
if(login!=null){
return SUCCESS;
}else {
return ERROR;
}
}
UseeDao
public class UseeDao {
public static Usee login(String name, String pwd) {
Session session = HibernateSessionFactory.getSession();
List<Usee> list = new ArrayList<>();
list = session.createQuery("from Usee u where u.name=? and u.password=?").setParameter(0, name).setParameter(1, pwd).list();
if (list.size() != 0) {
Usee usee = list.get(0);
return usee;
} else {
return null;
}
}
}
Usee.java
public class Usee implements Serializable {
private int id;
private String num;
private String name;
private String password;
public Usee(){}
@Id
@Column(name = "ID", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "NUM", nullable = true, length = 45)
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Basic
@Column(name = "NAME", nullable = true, length = 45)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "PASSWORD", nullable = true, length = 45)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Usee usee = (Usee) o;
return id == usee.id &&
Objects.equals(num, usee.num) &&
Objects.equals(name, usee.name) &&
Objects.equals(password, usee.password);
}
@Override
public int hashCode() {
return Objects.hash(id, num, name, password);
}
}
HibernateSessionFactory.java
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}