Results 1 to 7 of 7
  1. #1
    dariusaron is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default <html:form> struts form not working if it is inside a jsp under WEB-INF

    Hi guys ,

    I have a problem trying to process a form which is inside a jsp page under WEB-INF.
    My environment: NetBeans 6.9, GlassFish 3.1, Struts 1.3.8
    I took this example by reference:
    Introduction to the Struts Web Framework - NetBeans IDE 6.7/6.8/6.9 Tutorial
    In this example there is a struts <html:form> login form inside a jsp, outside WEB-INF directory, so the jsp can be directly accessed from the browser;
    In my application I need the jsp that contains the form, to be inside web-inf directory, but if I make the changes in this way I get the following scenario:

    After I do everything and I make the deployment, I do the following:
    - input the address: http://localhost:8080/MyStrutsApp/loginn and the login form appears
    - then I fill in some input, I press Login and I get a 404 Error

    Can you please have a look on my code and give some ideas?

    MyStrutsApp/WEB-INF/loginn.jsp:

    Java Code:
       1. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>  
       2. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>  
       3.   
       4. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
       5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
       6.     "http://www.w3.org/TR/html4/loose.dtd">  
       7.   
       8. <html>  
       9.     <head>  
      10.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
      11.         <link rel="stylesheet" type="text/css" href="stylesheet.css">  
      12.         <title>Login Form</title>  
      13.     </head>  
      14.     <body>  
      15.         <h1>Login Form</h1>  
      16.   
      17.         <html:form action="/login">  
      18.             <table border="0">  
      19.                 <tbody>  
      20.                     <tr>  
      21.                         <td colspan="2">  
      22.                             <bean:write name="LoginForm" property="error" filter="false"/>  
      23.                              </td>  
      24.                     </tr>  
      25.                     <tr>  
      26.                         <td>Enter your name:</td>  
      27.                         <td><html:text property="name" /></td>  
      28.                     </tr>  
      29.                     <tr>  
      30.                         <td>Enter your email:</td>  
      31.                         <td><html:text property="email" /></td>  
      32.                     </tr>  
      33.                     <tr>  
      34.                         <td></td>  
      35.                         <td><html:submit value="Login" /></td>  
      36.                     </tr>  
      37.                 </tbody>  
      38.             </table>  
      39.         </html:form>  
      40.     </body>  
      41. </html>
    MyStrutsApp/WEB-INF/success.jsp:

    Java Code:
       1. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>  
       2.   
       3. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
       4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
       5.     "http://www.w3.org/TR/html4/loose.dtd">  
       6.   
       7. <html>  
       8.     <head>  
       9.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
      10.         <link rel="stylesheet" type="text/css" href="stylesheet.css">  
      11.         <title>Login Success</title>  
      12.     </head>  
      13.     <body>  
      14.         <h1>Congratulations!</h1>  
      15.   
      16.         <p>You have successfully logged in.</p>  
      17.   
      18.         <p>Your name is: <bean:write name="LoginForm" property="name" />.</p>  
      19.   
      20.         <p>Your email address is: <bean:write name="LoginForm" property="email" />.</p>  
      21.     </body>  
      22. </html>
    MyStrutsApp/WEB-INF/web.xml:

    Java Code:
       1. <?xml version="1.0" encoding="UTF-8"?>  
       2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
       3.     <servlet>  
       4.         <servlet-name>action</servlet-name>  
       5.         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
       6.         <init-param>  
       7.             <param-name>config</param-name>  
       8.             <param-value>/WEB-INF/struts-config.xml</param-value>  
       9.         </init-param>  
      10.         <init-param>  
      11.             <param-name>debug</param-name>  
      12.             <param-value>2</param-value>  
      13.         </init-param>  
      14.         <init-param>  
      15.             <param-name>detail</param-name>  
      16.             <param-value>2</param-value>  
      17.         </init-param>  
      18.         <load-on-startup>2</load-on-startup>  
      19.         </servlet>  
      20.     <servlet-mapping>  
      21.         <servlet-name>action</servlet-name>  
      22.         <url-pattern>*.do</url-pattern>  
      23.         <url-pattern>/loginn</url-pattern>  
      24.     </servlet-mapping>  
      25.     <session-config>  
      26.         <session-timeout>  
      27.             30  
      28.         </session-timeout>  
      29.     </session-config>  
      30.     </web-app>
    MyStrutsApp/WEB-INF/struts-config.xml:

    Java Code:
       1. <?xml version="1.0" encoding="UTF-8" ?>  
       2.   
       3. <!DOCTYPE struts-config PUBLIC  
       4.           "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  
       5.           "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">  
       6.   
       7. <struts-config>  
       8.     <form-beans>  
       9.         <form-bean name="LoginForm" type="com.myapp.struts.LoginForm"/>  
      10.     </form-beans>  
      11.       
      12.     <global-exceptions>    
      13.     </global-exceptions>  
      14.   
      15.     <global-forwards>  
      16.         <forward name="welcome"  path="/Welcome.do"/>  
      17.     </global-forwards>  
      18.   
      19.     <action-mappings>  
      20.         <action path="/loginn" scope="request" type="com.myapp.struts.GetAction">  
      21.             <forward name="loginn" path="/WEB-INF/loginn.jsp"/>  
      22.         </action>  
      23.   
      24.         <action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false">  
      25.             <forward name="failure" path="/WEB-INF/loginn.jsp"/>  
      26.             <forward name="success" path="/WEB-INF/success.jsp"/>  
      27.         </action>  
      28.         <action path="/Welcome" forward="/welcomeStruts.jsp"/>  
      29.     </action-mappings>  
      30.       
      31.     <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>  
      32.   
      33.     <message-resources parameter="com/myapp/struts/ApplicationResource"/>  
      34.       
      35.     <plug-in className="org.apache.struts.tiles.TilesPlugin" >  
      36.         <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />  
      37.         <set-property property="moduleAware" value="true" />  
      38.     </plug-in>  
      39.       
      40.     <!-- ========================= Validator plugin ================================= -->  
      41.     <plug-in className="org.apache.struts.validator.ValidatorPlugIn">  
      42.         <set-property  
      43.             property="pathnames"  
      44.             value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>  
      45.     </plug-in>  
      46.     
      47. </struts-config>
    LoginForm.java

    - this bean is implemented in package com.myapp.struts; I can list it if you need

    LoginAction.java

    Java Code:
       1. package com.myapp.struts;  
       2.   
       3. import javax.servlet.http.HttpServletRequest;  
       4. import javax.servlet.http.HttpServletResponse;  
       5. import org.apache.struts.action.Action;  
       6. import org.apache.struts.action.ActionForm;  
       7. import org.apache.struts.action.ActionForward;  
       8. import org.apache.struts.action.ActionMapping;  
       9.   
      10. public class LoginAction extends org.apache.struts.action.Action {  
      11.   
      12.     private static final String SUCCESS = "success";  
      13.     private final static String FAILURE = "failure";  
      14.   
      15.     @Override  
      16.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
      17.             HttpServletRequest request, HttpServletResponse response)  
      18.             throws Exception {  
      19.   
      20.         // extract user data  
      21.         LoginForm formBean = (LoginForm) form;  
      22.         String name = formBean.getName();  
      23.         String email = formBean.getEmail();  
      24.   
      25.         // perform validation  
      26.         if ((name == null) || // name parameter does not exist  
      27.                 email == null || // email parameter does not exist  
      28.                 name.equals("") || // name parameter is empty  
      29.                 email.indexOf("@") == -1) {   // email lacks '@'  
      30.   
      31.             formBean.setError();  
      32.             return mapping.findForward(FAILURE);  
      33.         }  
      34.   
      35.         return mapping.findForward(SUCCESS);  
      36.     }  
      37. }
    GetAction.java

    Java Code:
       1. package com.myapp.struts;  
       2.   
       3. import javax.servlet.http.HttpServletRequest;  
       4. import javax.servlet.http.HttpServletResponse;  
       5. import org.apache.struts.action.ActionForm;  
       6. import org.apache.struts.action.ActionForward;  
       7. import org.apache.struts.action.ActionMapping;  
       8.   
       9. public class GetAction extends org.apache.struts.action.Action {  
      10.       
      11.     @Override  
      12.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
      13.             HttpServletRequest request, HttpServletResponse response)  
      14.             throws Exception {  
      15.           
      16.         return mapping.findForward("loginn");  
      17.     }  
      18. }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    I might be being blind, but I don't see a mapping for login.
    Java Code:
    <html:form action="/login">

  3. #3
    dariusaron is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Please watch inside struts-config.xml, you will see it:
    24. <action name="LoginForm" path="/login" ...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    Ah!
    I am blind...:)

    Except you haven't got a servlet mapping have you?
    The only ones I can see that will map to Struts are *.do and loginn.

  5. #5
    dariusaron is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    "/login" action from loginn.jsp's form will map automatically to a "do", which I already included through "*.do" from web.xml, so everything is ok there but my problem is not solved yet

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    (You can tell I haven't done Struts for a few years...:))

    What happens when you debug it?
    Do you get into the Action?

  7. #7
    dariusaron is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Finally Solved!!!
    In web.xml deployment descriptor, it is only ok a mapping like *.something. I also added /loginn which was ok for the beginning because I could load the loginn.jsp page. But after that I was asking from the html:form, for the action named "/login" which was matching the second pattern (because it ALWAYS matches the last pattern) which begins with "/" and it didn't go for "*.do" and after that everything was a mess.
    So, the solutions would look like this:

    1.
    Java Code:
         <servlet-mapping>  
             <servlet-name>action</servlet-name>  
             <url-pattern>/loginn</url-pattern>
             <url-pattern>*.do</url-pattern>  
         </servlet-mapping>
    or

    2.
    Java Code:
         <servlet-mapping>  
             <servlet-name>action</servlet-name>  
             <url-pattern>*.do</url-pattern>  
         </servlet-mapping>
    and another adjustments in the Action Class

Similar Threads

  1. HTML Form
    By johnmergene in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:23 AM
  2. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  3. enctype=multipart/form-data with form data in struts
    By vk_satheesh in forum New To Java
    Replies: 0
    Last Post: 09-19-2008, 12:48 PM
  4. HTML Form
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:37 PM
  5. [SOLVED] Internal frames inside a MDI form
    By Melki in forum AWT / Swing
    Replies: 3
    Last Post: 07-01-2008, 06:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •