springxml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>ssh02</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!--配置struts过滤器 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping><!-- 配置spring监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param>
</web-app>
UserAction 类
package com.qianfeng.action;import com.qianfeng.dao.UserDao;
import com.qianfeng.entity.User;public class UserAction {private User u = new User();private UserDao ud;public UserDao getUd() {return ud;}public void setUd(UserDao ud) {this.ud = ud;}public void setU(User u) {this.u = u;}public User getU() {return u;}public String login() {String str = "index";if (ud.login(u)) {str = "ss";}return str;}}
UserDao类
package com.qianfeng.dao;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;import com.qianfeng.entity.User;public class UserDao {private SessionFactory sf;public void setSf(SessionFactory sf) {this.sf = sf;}public boolean login(User u) {Session session = sf.openSession();Query query = session.createQuery("from User where acount=? and pwd=?");query.setString(0, u.getAcount());query.setString(1, u.getPwd());Object rs = query.uniqueResult();session.close();return rs == null ? false : true;}
}
User类
package com.qianfeng.entity;import java.io.Serializable;public class User implements Serializable {private int id;private String acount;private String pwd;public User() {super();}public String getAcount() {return acount;}public User(String acount, String pwd) {super();this.acount = acount;this.pwd = pwd;}public void setAcount(String acount) {this.acount = acount;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public int getId() {return id;}public void setId(int id) {this.id = id;}}
login 登录界面
ss登录成功界面