1 login.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/frameset.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<STYLE type=text/css>
BODY {
FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋体
}
TD {
FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋体
}
</STYLE> <META content="MSHTML 6.00.6000.16809" name=GENERATOR></HEAD>
<BODY>
<FORM id=form1 name=form1 action="${pageContext.request.contextPath }/UserAction_login" method=post> <DIV id=UpdatePanel1>
<DIV id=div1
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>
<DIV id=div2
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV> <DIV>&nbsp;&nbsp; </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=900 align=center border=0>
<TBODY>
<TR>
<TD style="HEIGHT: 105px"><IMG src="data:images/login_1.gif"
border=0></TD></TR>
<TR>
<TD background=images/login_2.jpg height=300>
<TABLE height=300 cellPadding=0 width=900 border=0>
<TBODY>
<TR>
<TD colSpan=2 height=35></TD></TR>
<TR>
<TD width=360></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=2 border=0>
<TBODY>
<TR>
<TD style="HEIGHT: 28px" width=80>登 录 名:</TD>
<TD style="HEIGHT: 28px" width=150><INPUT id=txtName
style="WIDTH: 130px" name="user_code"></TD>
<TD style="HEIGHT: 28px" width=370><SPAN
id=RequiredFieldValidator3
style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入登录名</SPAN></TD></TR>
<TR>
<TD style="HEIGHT: 28px">登录密码:</TD>
<TD style="HEIGHT: 28px"><INPUT id=txtPwd style="WIDTH: 130px"
type=password name="user_password"></TD>
<TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4
style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入密码</SPAN></TD></TR>
<TR>
<TD style="HEIGHT: 28px">验证码:</TD>
<TD style="HEIGHT: 28px"><INPUT id=txtcode
style="WIDTH: 130px" name=txtcode></TD>
<TD style="HEIGHT: 28px">&nbsp;</TD></TR>
<TR>
<TD style="HEIGHT: 18px"></TD>
<TD style="HEIGHT: 18px"><span style="color:red"><s:property value="exception.message"/></span></TD>
<TD style="HEIGHT: 18px"></TD></TR>
<TR>
<TD></TD>
<TD><INPUT id=btn
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
type=image src="data:images/login_button.gif" name=btn>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD><IMG src="data:images/login_3.jpg"
border=0></TD></TR></TBODY></TABLE></DIV></DIV>
</FORM>
<s:debug></s:debug>
</BODY></HTML>

2 UserAction

package www.test.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import www.test.domain.User;
import www.test.service.UserService;
import www.test.service.impl.UserServiceImpl; public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
private UserService us = new UserServiceImpl();
public String login() throws Exception {
//1 调用Service 执行登陆操作
User u = us.login(user);
//2 将返回的User对象放入session域作为登陆标识
ActionContext.getContext().getSession().put("user", u);
//3 重定向到项目的首页
return "toHome";
}
@Override
public User getModel() {
return user;
}
}

3 UserServiceImpl

package www.test.service.impl;

import www.test.dao.UserDao;
import www.test.dao.impl.UserDaoImpl;
import www.test.domain.User;
import www.test.service.UserService;
import www.test.utils.HibernateUtils; public class UserServiceImpl implements UserService {
private UserDao ud = new UserDaoImpl();
@Override
public User login(User user) {
//打开事务
HibernateUtils.getCurrentSession().beginTransaction();
//1.调用Dao根据登陆名称查询User对象
User existU = ud .getByUserCode(user.getUser_code());
//提交事务
HibernateUtils.getCurrentSession().getTransaction().commit(); if(existU==null){
//获得不到=>抛出异常提示用户名不存在
throw new RuntimeException("用户名不存在!");
}
//2 比对密码是否一致
if(!existU.getUser_password().equals(user.getUser_password())){
//不一致=>抛出异常提示密码错误
throw new RuntimeException("密码错误!");
}
//3 将数据库查询的User返回
return existU;
}
}

4 UserDaoImpl

package www.test.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session; import www.test.dao.UserDao;
import www.test.domain.User;
import www.test.utils.HibernateUtils; public class UserDaoImpl implements UserDao { @Override
public User getByUserCode(String user_code) {
//HQL查询
//1.获得Session
Session session = HibernateUtils.getCurrentSession();
//2 书写HQL
String hql = "from User where user_code = ? ";
//3 创建查询对象
Query query = session.createQuery(hql);
//4 设置参数
query.setParameter(0, user_code);
//5 执行查询
User u = (User) query.uniqueResult();
return u;
} }

5 struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
-->
<constant name="struts.devMode" value="true"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<global-exception-mappings>
<!-- 如果出现名为java.lang.RuntimeException的异常,就跳转到名为error的结果 -->
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="CustomerAction_*" class="www.test.web.action.CustomerAction" method="{1}" >
<result name="list" >/jsp/customer/list.jsp</result>
<result name="toList" type="redirectAction">
<param name="actionName">CustomerAction_list</param>
<param name="namespace">/</param>
</result>
</action>
<action name="UserAction_*" class="www.test.web.action.UserAction" method="{1}" >
<result name="toHome" type="redirect">/index.htm</result>
<result name="error" type="dispatcher">/login.jsp</result>
</action>
</package>
</struts>

最新文章

  1. logback 常用配置详解&lt;appender&gt;
  2. Mac 显示隐藏文件
  3. iOS 数据库的增删改查(OC版)
  4. RAID级别
  5. GD库处理图像
  6. 冒泡排序,sql分页语句
  7. Spring MVC和Struts2的比较(二)
  8. SSDT Hook实现简单的进程隐藏和保护【转载】
  9. 转:值得推荐的C/C++框架和库(真的很强大)
  10. PHP常用正则表达式汇总 [复制链接]
  11. Maven聚合与继承
  12. 设置 Eclipse 智能代码提示,大幅度减少 alt+/ 使用频率,打每个字都出现代码提示的办法
  13. 12个非常不错的免费HTML后台管理模板
  14. Javascript模块化编程之难处
  15. webstorm常用快捷键及插件
  16. CDH集群搭建部署
  17. BigInteger详解
  18. 从0到上线开发企业级电商项目_前端_01_sublime使用技巧
  19. java中equals和==以及toString
  20. Linux Samba服务主配文件smb.conf中文详解【转】

热门文章

  1. Sql--IDENTITY()自动增长列
  2. java中公用类型Car必须在它自己的文件中定义
  3. IOS 防坑指南
  4. SQLServer如何在批量插入后,获取批量插入的自增列的值
  5. WinForm中DataGridView的使用(五) - 自定义列
  6. java并发编程(更新)
  7. Mybatis中的连接池
  8. k8s(未完待续)
  9. requests库和urllib包对比
  10. SDUT OJ 数据结构实验之链表六:有序链表的建立