为什么我用模型驱动获取不到登录页面输入的用户名和密码,而且输入正确的用户名和密码都只能调到登录失败的页面?
UserAction.java
public class UserAction extends ActionSupport implements ModelDriven<User>{
//模型驱动使用的对象
private User user = new User();
/**用于接收jsp获得的参数,这样获得的用户名和密码就会封装到user这个对象当中。
* @return
*/
@Override
public User getModel() {
return user;
}
//注入业务层的类,并提供一个setter方法
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/**用户登录的执行方法
* @return
*/
public String login(){
System.out.println("login方法执行了");
System.out.println(user.getUsername()+" "+user.getPassword());
//调用业务层的类
User existUser = userService.login(user);
if(existUser == null){
//登录失败
this.addActionError("用户名或密码不正确!");
return INPUT;
}else{
//登录成功
//把用户信息存入到session当中
ActionContext.getContext().getSession().put("existUser", existUser);
return SUCCESS;
}
}
UserServiceImpl.java
/*
* 用户业务层实现类
*/
public class UserServiceImpl implements UserService {
//注入DAO的类,并提供一个setter方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/*
* 业务层登录的方法
*/
@Override
public User login(User user) {
User existUser = userDao.findByUsernameAndPassword(user);
return existUser;
}
}
UserDaoImpl.java
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
/**
* DAO中根据用户名和密码查询用户的方法
*/
@Override
public User findByUsernameAndPassword(User user) {
String hql = "from User where username = ? and password = ?";
List<User> list = (List<User>) this.getHibernateTemplate().find(hql, user.getUsername(),user.getPassword());
if(list.size() > 0){
return list.get(0);
}
return null;
}
}
登录页面index.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登录页面</title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<script type="text/javascript" language='javascript' src="js/index.js"></script>
</head>
<body>
<!-- contact-form -->
<div class="message warning">
<div class="inset">
<div class="login-head">
<h1>欢迎登录</h1>
</div>
<h3><s:actionerror/></h3>
<s:form action="user_login" method="post" namespace="/">
<li>
<input type="text" id="username" class="text" value="用户名" ><a href="#" class="icon user"></a>
</li>
<div class="clear"> </div>
<li>
<input type="password" id="password" value="Password"> <a href="#" class="icon lock"></a>
</li>
<div class="clear"> </div>
<div class="submit">
<input type="submit" id="login" value="登录" >
<h4><a href="sign.jsp">注册</a></h4>
<div class="clear"> </div>
</div>
</s:form>
</div>
</div>
<div class="clear"> </div>
</body>
</html>
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>
<package name="luntan" extends="struts-default" namespace="/">
<action name="user_*" class="userAction" method="{1}">
<result name="input">/index.jsp</result>
<result name="success" type="redirect">/default.jsp</result>
</action>
</package>
</struts>