登录页面Login把用户提交
<%@ page contentType="text/html; charset=GBK" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
  <head>
  <title>
  用户登录页面
  </title>
  <html:base/>
  </head>
  <body bgcolor="#ffffff"><p>
  <center>
    <h2>用户登录页面</h2><p>
    <html:errors/><p>
      <html:form action="/loginAction.do" focus="userName">
        姓名:<html:text property="userName" size="16" maxlength="16">
        </html:text>
        密码:<html:password property="password" size="16" maxlength="16">
        </html:password>
        <html:submit property="submit" value="提交"/>
        <html:reset property="reset" value="重置"/>
      </html:form>
  </body>
</html:html>在FormBean中(LoginForm.java)设好相同的属性(省略方法):
    private String userName;
    private String password;在Action中(LoginAction.java)的execute方法内容:
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        LoginForm loginForm = (LoginForm) form;
        String userName=loginForm.getUserName();
        String password=loginForm.getPassword();
        LoginDAO dao=new LoginDAO();
        User user=new User(userName,password);
        if(dao.login(userName,password)){            request.setAttribute("userbean",user);  //在这设置了提交的对象,为什么在跳转后的页面读取不出来??            return(mapping.findForward("Success"));
        }else{
            return(mapping.findForward("Failure"));
        }
    }其他的Bean有:
LoginDAO.java:
package loginstruts;import java.sql.*;
public class LoginDAO {    Connection conn=null;    public LoginDAO() {
    }    public Connection getConnectionDB(){
        String strDB="sun.jdbc.odbc.JdbcOdbcDriver";
        String strUrl="jdbc:odbc:login";
        try {
            Class.forName(strDB);
            conn=DriverManager.getConnection(strUrl);
            System.out.println("connetin seccess......");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return conn;
    }    public void closeConnectionDB(){
        try {
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }    public boolean login(String u,String p){
        User user=new User(u,p);
        String strSql="select * from user";
        getConnectionDB();
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(strSql);
            while(rs.next()){
                user.setPassword(rs.getString("Password"));
                user.setUserName(rs.getString("UserName"));
                if(u.equals(user.getUserName()) || (p.equals(user.getPassword()))){
                    rs.close();
                    st.close();
                    closeConnectionDB();
                    return true;
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }        return false;
    }
}User:
package loginstruts;public class User {
    private String userName;
    private String password;    public User(){    }
    public User(String name,String psw) {
        this.userName=name;
        this.password=psw;
    }    public void setUserName(String userName) {
        this.userName = userName;
    }    public void setPassword(String password) {
        this.password = password;
    }    public String getUserName() {
        return userName;
    }    public String getPassword() {
        return password;
    }
}
struts-config.xml中的配置(部分):
<struts-config>
  <form-beans>
    <form-bean name="LoginForm" type="loginstruts.LoginForm"/>
  </form-beans>  <action-mappings>
    <action    path      = "/loginAction"
               type      = "loginstruts.LoginAction"
               name      = "LoginForm"
               scope     = "request"
               input     = "/Login.jsp">
      <forward name="Success" path="/Welcome.jsp" redirect="true" />
      <forward name="Failure" path="/Login.jsp" redirect="true" />
    </action>
  </action-mappings>
</struts-config>最后我在跳转后的Welcome.jsp页面中就是读不出request中的数据出来,为什么呢?
<%@ page contentType="text/html; charset=GBK" language="java" %>
<%@ page import="loginstruts.*" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><html:html locale="true">
  <head>
  <title>  用户登录成功  </title>
  <html:base/>
  </head>
  
  <body bgcolor="#ffffff"><p>
  <center>
  </center>
    <h2>登录成功</h2><p>
    
    <logic:present name="userbean"  scope="request">
      <h2>Hello,
      <bean:write name="userbean" property="name" />!<p>
    </h2>
    </logic:present>  </body>
</html:html>也试过在Welcome.jsp中用如下代码读出来的request.getAttribute("userbean")中的值为null....为什么呢?是不是哪里错了?求教...
<h1>
  <%
  User tb=(User)request.getAttribute("userbean");
  String name=tb.getUserName();
  out.println(name);
  %>
</h>

解决方案 »

  1.   

    改了一下Action(LoginAction.java)的execute方法内容,在方法中可以正确读出来:
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception{
            LoginForm loginForm = (LoginForm) form;
            String userName=loginForm.getUserName();
            String password=loginForm.getPassword();
            LoginDAO dao=new LoginDAO();
            User user=new User(userName,password);        if(dao.login(userName,password)){            request.setAttribute("userbean",user);  //在这设置了提交的对象,为什么在跳转后的页面读取不出来??            User user1=(User)request.getAttribute("userbean");
                System.out.println(user1.getUserName());  //在这里可以正确读出来用户名.            return(mapping.findForward("Success"));
            }else{
                return(mapping.findForward("Failure"));
            }
        }
      

  2.   

    <action-mappings>
        <action    path      = "/loginAction"
                   type      = "loginstruts.LoginAction"
                   name      = "LoginForm"
                   scope     = "request"
                   input     = "/Login.jsp">
    這裡的SCOPE是"request",使得你的formbean的生命周期在LoginAction業務處理之内,業務處理完畢以後就調用formbean的reset方法。
    ---------------------------
     System.out.println(user1.getUserName());  //在这里可以正确读出来用户名.
    在業務處理内部是可以讀出來的。
    我們做的時候每個jsp都要對應一個formbean。我也是剛剛學起,只知道這些,不一定正確。至於你的問題我還不清楚。
      

  3.   

    <forward name="Success" path="/Welcome.jsp" redirect="true" />
          <forward name="Failure" path="/Login.jsp" redirect="true" />
    redirect指的是是否重定向(重定向的话就不在同一个request中了)
    把redirect改为"false"即可
      

  4.   

    不是这个问题,域是request,属同一个request是应该可以读出来的。在这里没必要设会话级session
      

  5.   

    <forward name="Success" path="/Welcome.jsp" redirect="true" />
          <forward name="Failure" path="/Login.jsp" redirect="true" />
    redirect指的是是否重定向(重定向的话就不在同一个request中了)
    把redirect改为"false"即可
    改为false会出错,我再试试看...
      

  6.   

    你需要确定你这行代码是否执行,如果执行了,在页面上是不会有NULL的
      request.setAttribute("userbean",user);
      

  7.   

    根据我上面写的在execute方法中测试过,下面语句执行了,并存储有对象的值
      request.setAttribute("userbean",user);问题已解决:
    1、struts-config.xml中的重定向需设置为false
    2、在Welcome.jsp中的<bean:write name="userbean" property="name" />是自己写错了,我的User.java这个Bean中没有name属性,其实是userName,所以改为<bean:write name="userbean" property="userName" />,这样就可以读出来了。