当前位置: 代码迷 >> J2EE >> struts1 action传值给jsp页面,该如何解决
  详细解决方案

struts1 action传值给jsp页面,该如何解决

热度:19   发布时间:2016-04-21 21:58:42.0
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>
        <input type="button" class="button" value="Sign In" onclick="checkSubmit()" />
      </p>
      <p>
        <input type="button" class="button" value="Register" onclick="javascript:location.href='userRegister.jsp'"/>
      </p>
      
    </form>
}

------解决方案--------------------
action 中配置两个forward 一个正确页面,一个error 页面
如果错误action 中将错误信息放在session 中,
在登陆界面显示就行了,
也可用struts 的验证框架完成 。

------解决方案--------------------
struct1 action传值只需 request.setAttribute("key",value)就行了

jsp上${key}就可以获得。。
------解决方案--------------------
你应该在action中,定义:
private String key;(然后写上get,set)。
然后把public ActionForward execute改为public String execute,
execute方法最后return “success”。
你的return mapping.findForward("success");改为:key = "/fail.jsp";和key = "/success.jsp"
最后,在struts.xml中的rerult是<result name="success">${uri}</result>

这样自动能找到对应的成功页和失败页。
------解决方案--------------------
引用:
你应该在action中,定义:
private String key;(然后写上get,set)。
然后把public ActionForward execute改为public String execute,
  相关解决方案