You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by 友信 徐 <fr...@yahoo.com.cn> on 2007/06/13 07:39:19 UTC

Question in writing Struts Program

Hello,everybody.
   Recently,I wrote a simple JSP Web program basing on the Struts architecture.It created a page for a user to input his name and password,and after the user click the submit botton on the form,it can redirect to another page and show the name and password that the use has input on the page.I have completed the program but it can't work properly.
  I listed each part of the program as following:
   
   
  (a) TestForm.jsp
  <%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   
  <head>
  <title>Login Interface</title>
  </head>
  <body vLink="#006666" link="#003366" bgColor="#E0F0F8">
  <img height="33" src="image/enter.gif" width="148"> 
  <form action="ReadTestForm.do" method="post">
  UserName:
   <input size="15"name="PersonName"><p>
  Password:
   <input type="password" size="15" name="Psw"><p>
  <input type="submit" value="Submit">
  </form>
   
   
   
  (b) MTestForm.java
  package Test;
   
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionMapping;
  import javax.servlet.http.HttpServletRequest;
  import org.apache.struts.action.ActionErrors;
  import org.apache.struts.action.ActionMessage;
   
  public class MTestForm extends ActionForm{
   
          private String PersonName = null;
          private String Psw        = null;
          public  MTestForm(){}
          
  public void setPersonName(String name) {
                  this.PersonName = name;
          }  
          public String getPersonName() {
                  return PersonName;
          }  
          public void setPsw(String psw) {
                  this.Psw = psw;
          }  
          public String getPsw() {
                  return Psw;
          }
          
          public void reset(ActionMapping mapping,
                  HttpServletRequest request) {
                  this.PersonName = null;
                  this.Psw = null;
          }
          
  }
   
   
   
  (c) ReadTestFormAction.java
  package Test;
   
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
   
  import org.apache.struts.action.ActionMessages;
  import org.apache.struts.action.ActionMessage;
   
  import javax.servlet.ServletContext;
  import javax.sql.DataSource;
  import javax.servlet.http.*;
   
  public final class ReadTestFormAction extends Action{
   
          public ActionForward execute(
                          ActionMapping mapping,
                          ActionForm form,
                          HttpServletRequest request,  
                          HttpServletResponse response) throws Exception {
                          
                  
                       MTestForm TestFormBean = (MTestForm) form;         
          String PersonName = TestFormBean.getPersonName();
          String Psw = TestFormBean.getPsw();
                          
                          
          return mapping.findForward("ReadTestFormOk");
          }
  }
   
   
   
  (d) ShowForm.jsp
  <%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <%@ page import = "classmate.*" %>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Read Test Form and Show the Data</title>
  </head>
  <body>
  <%
          MTestForm ReadformBean1 = (MTestForm)request.getAttribute("TestFormBean1");
  %>
  <h1><img src="image/smile.gif">
  Welcome
  <%=ReadformBean1.getPersonName()%> 
  Your Password is:
  <%=ReadformBean1.getPsw()%>
   
  </h1><br>
  </body>
  </html>
   
   
   
  (e) web.xml
  <?xml version="1.0" encoding="ISO-8859-1"?>
   
  <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
   
  <web-app>
   
    <!-- Action Servlet Configuration -->
    <servlet>
      <servlet-name>actionServlet</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    </servlet>
   
    <!-- Action Servlet Mapping -->
    <servlet-mapping>
      <servlet-name>actionServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
    </servlet-mapping>
   
    <!-- The Welcome File List -->
    <welcome-file-list>
      <welcome-file>TestForm.jsp</welcome-file>
    </welcome-file-list>
   
  </web-app>
   
   
   
  (f)  struts-config.xml
  <?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE struts-config PUBLIC 
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" 
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
          
  <struts-config>
   
  <form-beans>
  <form-bean name="formBean1" type="Test.UserForm"/>
  <form-bean   name="TestFormBean1" type="Test.MTestForm">
  </form-bean>
   
   
          <action-mappings>
                  <action path="/login" type="Test.LoginAction" name="formBean1" scope="request" input="/login.jsp" >
                          <forward name="failed" path="/error.jsp"></forward>
                          <forward name="successed" path="/right.jsp"></forward>
                  </action>
                  <action path="/regist" forward="/regist.jsp"/>
  <action path="/ReadTestForm"
                  type="classmate.ReadTestFormAction" name="TestFormBean1" scope="request" input="/login.jsp">
          <forward name="ReadTestFormOk" path="/ReadForm.jsp"></forward>
  </action>
          </action-mappings>
          
  </struts-config>
   
      
   
           
             After I accessed the page of  "TestForm.jsp" and input the " Username and Password",click "Submit " button ,it can redirect to the  page of "ShowForm.jsp",but each time both the Username and Password  are "Null", It meaned that the statement {which is MTestForm ReadformBean1 = (MTestForm)request.getAttribute("TestFormBean1");
  }  and its following statement in the "ShowForm.jsp" can't accessed what the user have input for the Textboxs of Username and Password.
  I have tried and tried  and modify the program for many times,but it have not been solved.
  Can you help me  to  check out what is the wrong with the program?
  Thanks you for help and  Regards
   
   
  (Can you anybody tell me how edit the text so that it can been printed out clearly in this forum?)

 	      
---------------------------------
抢注雅虎免费邮箱3.5G容量,20M附件! 

Re: Question in writing Struts Program

Posted by "Henry F. Camacho Jr." <hf...@unpluggedcities.com>.
Looks to me when you hit your action you are not saving those attributes
in request.

request.setAttribute("username", PersonName);

Then in your JSP you can get the Attribute using getAttribute.

However I don't think you want to do this. Coming out of the action you
should call your business logic to determine if the login was
successful, and it looks like you have the code correct to get the
information from the form. Looking at your code I see the following in
the action:

String PersonName = TestFormBean.getPersonName();
String Psw = TestFormBean.getPsw()

Add something like:

BusinessLogic bl = new BusinessLogic();
try
{
	bl.testLogin(PersonName, Psw);
}
catch (BusinessLogicException e)
{
	return(mapping.findForward("login failed");
}

return(mapping.findForward("success");

Something like this.  I am pretty new at Struts however.

HFC


友信 徐 wrote:
> Hello,everybody.
>    Recently,I wrote a simple JSP Web program basing on the Struts architecture.It created a page for a user to input his name and password,and after the user click the submit botton on the form,it can redirect to another page and show the name and password that the use has input on the page.I have completed the program but it can't work properly.
>   I listed each part of the program as following:
>    
>    
>   (a) TestForm.jsp
>   <%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
>   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>    
>   <head>
>   <title>Login Interface</title>
>   </head>
>   <body vLink="#006666" link="#003366" bgColor="#E0F0F8">
>   <img height="33" src="image/enter.gif" width="148"> 
>   <form action="ReadTestForm.do" method="post">
>   UserName:
>    <input size="15"name="PersonName"><p>
>   Password:
>    <input type="password" size="15" name="Psw"><p>
>   <input type="submit" value="Submit">
>   </form>
>    
>    
>    
>   (b) MTestForm.java
>   package Test;
>    
>   import org.apache.struts.action.ActionForm;
>   import org.apache.struts.action.ActionMapping;
>   import javax.servlet.http.HttpServletRequest;
>   import org.apache.struts.action.ActionErrors;
>   import org.apache.struts.action.ActionMessage;
>    
>   public class MTestForm extends ActionForm{
>    
>           private String PersonName = null;
>           private String Psw        = null;
>           public  MTestForm(){}
>           
>   public void setPersonName(String name) {
>                   this.PersonName = name;
>           }  
>           public String getPersonName() {
>                   return PersonName;
>           }  
>           public void setPsw(String psw) {
>                   this.Psw = psw;
>           }  
>           public String getPsw() {
>                   return Psw;
>           }
>           
>           public void reset(ActionMapping mapping,
>                   HttpServletRequest request) {
>                   this.PersonName = null;
>                   this.Psw = null;
>           }
>           
>   }
>    
>    
>    
>   (c) ReadTestFormAction.java
>   package Test;
>    
>   import org.apache.struts.action.Action;
>   import org.apache.struts.action.ActionForm;
>   import org.apache.struts.action.ActionForward;
>   import org.apache.struts.action.ActionMapping;
>    
>   import org.apache.struts.action.ActionMessages;
>   import org.apache.struts.action.ActionMessage;
>    
>   import javax.servlet.ServletContext;
>   import javax.sql.DataSource;
>   import javax.servlet.http.*;
>    
>   public final class ReadTestFormAction extends Action{
>    
>           public ActionForward execute(
>                           ActionMapping mapping,
>                           ActionForm form,
>                           HttpServletRequest request,  
>                           HttpServletResponse response) throws Exception {
>                           
>                   
>                        MTestForm TestFormBean = (MTestForm) form;         
>           String PersonName = TestFormBean.getPersonName();
>           String Psw = TestFormBean.getPsw();
>                           
>                           
>           return mapping.findForward("ReadTestFormOk");
>           }
>   }
>    
>    
>    
>   (d) ShowForm.jsp
>   <%@ page language="java" contentType="text/html; charset=UTF-8"
>       pageEncoding="UTF-8"%>
>   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>   <%@ page import = "classmate.*" %>
>   <html>
>   <head>
>   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
>   <title>Read Test Form and Show the Data</title>
>   </head>
>   <body>
>   <%
>           MTestForm ReadformBean1 = (MTestForm)request.getAttribute("TestFormBean1");
>   %>
>   <h1><img src="image/smile.gif">
>   Welcome
>   <%=ReadformBean1.getPersonName()%> 
>   Your Password is:
>   <%=ReadformBean1.getPsw()%>
>    
>   </h1><br>
>   </body>
>   </html>
>    
>    
>    
>   (e) web.xml
>   <?xml version="1.0" encoding="ISO-8859-1"?>
>    
>   <!DOCTYPE web-app
>     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
>     "http://java.sun.com/dtd/web-app_2_3.dtd">
>    
>   <web-app>
>    
>     <!-- Action Servlet Configuration -->
>     <servlet>
>       <servlet-name>actionServlet</servlet-name>
>       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
>     </servlet>
>    
>     <!-- Action Servlet Mapping -->
>     <servlet-mapping>
>       <servlet-name>actionServlet</servlet-name>
>       <url-pattern>*.do</url-pattern>
>     </servlet-mapping>
>    
>     <!-- The Welcome File List -->
>     <welcome-file-list>
>       <welcome-file>TestForm.jsp</welcome-file>
>     </welcome-file-list>
>    
>   </web-app>
>    
>    
>    
>   (f)  struts-config.xml
>   <?xml version="1.0" encoding="ISO-8859-1" ?>
>   <!DOCTYPE struts-config PUBLIC 
>           "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" 
>           "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
>           
>   <struts-config>
>    
>   <form-beans>
>   <form-bean name="formBean1" type="Test.UserForm"/>
>   <form-bean   name="TestFormBean1" type="Test.MTestForm">
>   </form-bean>
>    
>    
>           <action-mappings>
>                   <action path="/login" type="Test.LoginAction" name="formBean1" scope="request" input="/login.jsp" >
>                           <forward name="failed" path="/error.jsp"></forward>
>                           <forward name="successed" path="/right.jsp"></forward>
>                   </action>
>                   <action path="/regist" forward="/regist.jsp"/>
>   <action path="/ReadTestForm"
>                   type="classmate.ReadTestFormAction" name="TestFormBean1" scope="request" input="/login.jsp">
>           <forward name="ReadTestFormOk" path="/ReadForm.jsp"></forward>
>   </action>
>           </action-mappings>
>           
>   </struts-config>
>    
>       
>    
>            
>              After I accessed the page of  "TestForm.jsp" and input the " Username and Password",click "Submit " button ,it can redirect to the  page of "ShowForm.jsp",but each time both the Username and Password  are "Null", It meaned that the statement {which is MTestForm ReadformBean1 = (MTestForm)request.getAttribute("TestFormBean1");
>   }  and its following statement in the "ShowForm.jsp" can't accessed what the user have input for the Textboxs of Username and Password.
>   I have tried and tried  and modify the program for many times,but it have not been solved.
>   Can you help me  to  check out what is the wrong with the program?
>   Thanks you for help and  Regards
>    
>    
>   (Can you anybody tell me how edit the text so that it can been printed out clearly in this forum?)
>
>  	      
> ---------------------------------
> 抢注雅虎免费邮箱3.5G容量,20M附件! 
>   

-- 
Henry F. Camacho Jr.
Unplugged Cities, LLC
800 Washington Ave No
Suite 501
Minneapolis, MN 55401
Fridley, MN  55432

763-235-3005 (Office)
763-257-6898 (Cell)
tknightowl (Skype)
hfc@unpluggedcities.com (email)
www.unpluggedcities.com (www)
KC0KUS (Amateur Radio)


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org