日期:2014-05-18  浏览次数:20718 次

struts1 action传值给jsp页面
刚开始学 什么都不会 做了个登录注册的页面 想在登录页面输入之后验证数据库中是否有信息,如果用户名密码正确 跳转到success页 如果不正确 返回登录页 并提示用户名密码不正确 
搜了无数个网页 实在不知道怎么写了。。。。恳请指点
action:
public class UserLoginAction extends Action {
Logger logger = Logger.getLogger(UserLoginAction.class);

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserLoginForm userLoginForm = (UserLoginForm) form;

String name = userLoginForm.getName();//从userLoginForm中获取用户名称
String password = userLoginForm.getPassword();//从userLoginForm中获取密码

DBConnect db = new DBConnect();//创建DBConnect对象
if (db.verifyLogin(name, password)){//验证用户名,密码在数据库中是否存在
return mapping.findForward("success");//如果存在,跳转到"success"对应的jsp页面
}

request.getSession().setAttribute("flag", false);

System.out.println("it's run here.");


return mapping.findForward("failure");//如果验证失败,跳转到"failure"对应的jsp页面

}




jsp: 
function check(flag){
   if(flag!="null"){ //第一次访问该页面时request.getAttribute("flag")一定为null值
    if(flag.length==0){ 
     alert('用户名或密码错误');
    }
   }
  }
</script>
<%
  String flag = (String) request.getAttribute("flag");
  request.setAttribute("flag", null);
 %>
<body id="login" onload="check('<%=flag%>')">
<div id="login-wrapper" class="png_bg">
  <div id="login-top">
    <h1>Simpla Admin</h1>
    <!-- Logo (221px width) -->
    <a href="userLogin.jsp"><img id="logo" src="images/other/logo.png" alt="Simpla Admin logo" /></a> </div>
    <!-- End #logn-top -->

  <div id="login-content">
    <form name="userLoginForm" action="userLoginAction.do" method="post">
 
      <div class="notification information png_bg">
        <div> Please enter your name and password. </div>
      
      </div>
            <p>
        <label>Username</label>
        <input class="text-input" type="text" name="name" maxlength="15"/>
      </p>
      <div class="clear"></div>
      <p>
        <label>Password</label>
        <input class="text-input" type="password" name="password"/>
      </p>
      <div class="clear"></div>
      <p>