<%@taglib prefix="s" uri="/struts-tags"%>
<HTML>
<HEAD>
<TITLE>登录成功页面</TITLE>
</HEAD>
<BODY>
<s:form action="Login" method="post" namespace="/">
<h3>用户登录</h3>
<s:textfield name="username" label="用户名:"/>
<s:textfield name="password" label="密码:"/>
<s:submit value="提交"/>
</s:form>
</BODY>
</HTML>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false"
/> <constant name="struts.devMode" value="false" /> <include
file="example.xml"/> <package name="default" namespace="/"
extends="struts-default"> <default-action-ref name="index" /> <action
name="index"> <result type="redirectAction"> <param
name="actionName">HelloWorld</param> <param
name="namespace">/example</param> </result> </action> </package>
-->
<!-- Add packages here -->
<constant name="struts.devMode" value="false" />
<constant name="structs.i18n.encoding" value="GBK"><!-- internationalization --></constant>
<!--
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
-->
<!-- 这个是这里要用的-->
<package name="default" extends="struts-default" namespace="/">
<!-- 下面的action里的class可以省略,默认调用的是ActionSupport -->
<action name="Login" class="com.wenbo.action.LoginAction" >
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/login_success.jsp</result>
</action>
</package>
</struts>
import com.opensymphony.xwork2.ModelDriven; //该接口提供模型驱动方法
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext; //该类提供Servlet API的访问
import com.wenbo.dao.userDao;
import com.wenbo.dao.impl.userDaoImpl;
import com.wenbo.entity.User;
import com.wenbo.entity.UserBean;
/*这个问题困扰了很多Struts2的初学者,我这里提供一些建议:
(1)请你统一整个系统中的Action使用的驱动模型,即要么都是用属性驱动,要么都是用模型驱动。
(2)如果你的DB中的持久层的对象与表单中的属性都是一一对应的话,那么就使用模型驱动吧,毕竟看起来代码要整洁得多。
(3)如果表单的属性不是一一对应的话,那么就应该使用属性驱动,否则,你的系统就必须提供两个Bean,一个对应表单提交的数据,另一个用与持久层*/
/**
* (1) 属性驱动的话,后台要从JSP页面取得用户输入的值的话,要使用 request.getParameter("tuoxie");
* 而驱动模型,则不用这样,直接就有个JavaBean对应这用户输入的值。
* */
public class LoginAction extends ActionSupport implements ModelDriven<UserBean>{
/**
*
*/
private static final long serialVersionUID = 1L;
//使用模型驱动
private UserBean userModel = new UserBean();
//实现ModelDriven接口必须实现的方法
public UserBean getModel(){
return this.userModel;
}
public String execute() throws Exception{
userDao userDao = new userDaoImpl();
User user = userDao.select(getModel().getUsername());
if(null != user ){
if(getModel().getPassword().equalsIgnoreCase(user.getPassword())){
getModel().setTip("服务器提示!!!");
//String user = (String) ActionContext.getContext().getSession().get("user");
ActionContext.getContext().getSession().put("user", getModel().getUsername());
return "success";
}
else{
getModel().setTip("输入用户名或密码错误!!!");
return "error";
}
}
else{
getModel().setTip("没有该用户!!!");
return "error";
}
}
}
public class UserBean {
private String username;
private String password;
private String tip;
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;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
}
import com.wenbo.entity.User;
public interface userDao {
//public void insert(UserBean user);
//public void update(UserBean user);
//public void delete(UserBean user);
public User select(String name);
//public Collection selectAll();
}
/**/
package com.wenbo.dao.impl;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.Session;
import com.wenbo.dao.userDao;
import com.wenbo.entity.User;
import com.wenbo.util.HibernateUtil;
public class userDaoImpl implements userDao {
private Session session = null;
private Transaction trans = null;
public User select(String name) {
// TODO Auto-generated method stub
User user = null;
try{
//HibernateUtil hibernateUtil = new HibernateUtil();
//session = hibernateUtil.getSession();
session = HibernateUtil.getSession();
trans = session.beginTransaction();
Query query = session.createQuery("from User u where u.username= ?" );
query.setString(0, name);
if(null != query.list() || !query.list().isEmpty()){
user = (User) query.list().get(0);
}
trans.commit();
}
catch(Exception e){
e.printStackTrace();
trans.rollback();
}
finally{
session.close();
}
return user;
}
}
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(int id,String username,String password){
this.id = id;
this.username = username;
this.password = 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;
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
/* public Session getSession(){
Configuration cfg = new Configuration();
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();//要子hibernate.cfg.xml中配置<property name="current_session_context_class">thread</property>
return session;
}
public static void main(String[] args){
HibernateUtil h = new HibernateUtil();
Session s= h.getSession();
System.out.println(s==null);
}
*/
private static SessionFactory factory;
static{
try{
Configuration cfg = new Configuration();
factory = cfg.configure().buildSessionFactory();
}
catch(HibernateException e){
System.out.println("111111111111111");
}
}
public static Session getSession(){
//System.out.println("0000000000");
return factory.openSession();
}
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- <class name="User" table="users">-->
<class name="com.wenbo.entity.User" table="users">
<id name = "id" column="id"/>
<property name="username" column="name" />
<property name="password" column="password" />
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">36335576</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property>-->
<!-- sessionFactory.getCurrentSession();-->
<!-- <property name="current_session_context_class">thread</property>-->
<!-- <mapping resource="com/bjsxt/hibernate/Student.hbm.xml" /> -->
<mapping resource="com/wenbo/util/User.hbm.xml"/>
<!-- <mapping class="com.bjsxt.hibernate.Teacher" />-->
</session-factory>
</hibernate-configuration>
<%@taglib prefix="s" uri="/struts-tags"%>
<HTML>
<HEAD>
<TITLE>登录页面</TITLE>
</HEAD>
<BODY>
<h3><s:property value="tip"/></h3>
欢迎,${sessionScope.user},您已经登录成功!
</BODY>
</HTML>