UserForm userform = new UserForm();和 UserForm userform = (UserForm) form;有什么区别啊?
我用前面的语句的话,在jsp页面中没有异常,但是得不到UserForm里面的值。而用后面一句就出现异常。
login.jsp<form action="login.do" method="post">
     <table border="1">
     <tr><td>用户名:<input type="text" name="name"></td></tr>
     <tr><td>密码:<input type="password" name="pwd"></td></tr>
     <tr><td colspan="2"><input type="submit" value="提交"></td></tr>
        </table>
    </form>struts-config.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
  <data-sources />
  <form-beans>
   <form-bean name="formBean1" type="com.Lee.struts.UserForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards>
   <forward name="successed" path="/right.jsp"></forward>
   <forward name="failed" path="/wrong.jsp"></forward>
  </global-forwards>
  <action-mappings>
   <action path="/regist" forward="/regist.jsp"></action>
   <action path="/login" type="com.Lee.struts.LoginAction" name="formBean1" scope="request" input="login.jsp"></action>
  </action-mappings>
  <message-resources parameter="com.Lee.struts.ApplicationResources" />
</struts-config>LoginAction.javapackage com.Lee.struts;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public final class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userform = new UserForm();
//UserForm userform = (UserForm) form;
String name = userform.getName();
String pwd = userform.getPwd();
HttpSession session = request.getSession();
session.setAttribute("name", name);
session.setAttribute("pwd", pwd);
if("111".equals(name)&&"111".equals(pwd)){
return mapping.findForward("successed");
}
else{ 
return mapping.findForward("failed");
}
}
}UserForm.java
package com.Lee.struts;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public final class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userform = new UserForm();
//UserForm userform = (UserForm) form;
String name = userform.getName();
String pwd = userform.getPwd();
HttpSession session = request.getSession();
session.setAttribute("name", name);
session.setAttribute("pwd", pwd);
if("111".equals(name)&&"111".equals(pwd)){
return mapping.findForward("successed");
}
else{ 
return mapping.findForward("failed");
}
}
}
wrong.jsp<body>
    wrong<br>
    <%=session.getAttribute("name")%>
    //LoginAction里面用UserForm userform=new UserForm();这里显示null,
    //LoginAction里面用UserForm userform = (UserForm) form;页面就会抛出异常。

    <%=session.getAttribute("pwd")%>
  </body>我不知道问题出在哪里,大家帮我看看,谢谢了。有分再加!!

解决方案 »

  1.   

    UserForm userform = new UserForm();是新创建个对象赋值给userform 
    UserForm userform = (UserForm) form;是将form赋值给userform 
    Action中要用UserForm userform = (UserForm) form;form中存着前台传过来的值。
    先要看看Action中能不能得到name和pwd的值
      

  2.   

    谢谢!!
    区别我明白了,用了UserForm userform = (UserForm) form;后出现下面的错误type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    root cause java.lang.NullPointerException
    com.Lee.struts.LoginAction.execute(LoginAction.java:17)//String name = userform.getName();
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.
      

  3.   

    你原先的这个UserForm有没继承自ActionForm呢?报的错误是类型转化错误,不会没继承吧??
      

  4.   


    继承了啊。1楼有代码。
    或者看下面的
    UserForm.java
    package com.Lee.struts;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;public final class LoginAction extends Action{
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            UserForm userform = new UserForm();
            //UserForm userform = (UserForm) form;
            String name = userform.getName();
            String pwd = userform.getPwd();
            HttpSession session = request.getSession();
            session.setAttribute("name", name);
            session.setAttribute("pwd", pwd);
            if("111".equals(name)&&"111".equals(pwd)){
                return mapping.findForward("successed");
            }
            else{ 
                return mapping.findForward("failed");
            }
        }
    }
      

  5.   


    你的UserForm.java
    就是像你贴出来的一样吗?
    你贴出来的分明是Action嘛
      

  6.   

    晕,不好意思。。贴错了package com.Lee.struts;import org.apache.struts.action.ActionForm;public class UserForm extends ActionForm{ /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name=null;
    private String pwd=null;
    UserForm(){

    }
    public void setName(String name){
    this.name=name;
    }
    public String getName(){
    return name;
    }
    public void setPwd(String pwd){
    this.pwd=pwd;
    }
    public String getPwd(){
    return pwd;
    }
    }
      

  7.   

    你试一下,修改一下页面:如下login.jsp<form action="login.do" method="post">
            <table border="1">
                <tr><td>用户名:<input type="text" name="name" value=""></td></tr>
                <tr><td>密码:<input type="password" name="pwd" value=""></td></tr>
                <tr><td colspan="2"><input type="submit" value="提交"></td></tr>
               </table>
        </form>
      

  8.   

    //UserForm userform = (UserForm) form;
    注释去掉
      

  9.   

    你用来接受的是这个action里面的form
    你需要把他强转成你需要的类型,所以。你action里面的//UserForm userform = (UserForm) form; 
    注释去掉
      

  10.   

    问题解决了。。居然是web.xml没有配置好,把那个start 0 那项去了。就可以了!!!
    谢谢楼上热心的朋友!结贴