Prepopulate form in Struts1
Asked Answered
I

2

5

I have a jsp as my view, which displays a form for adding a new user/ updating the user, if one is selected. I can't figure out how to prepopulate my form if a user is selected. I read about the solution using 2 actions, with the same form, one of which is just used to populate the fields, and on for submitting the data. However, this doesn't work for me, as my action (the one defined in action attribute for the form) isn't called when loading the jsp (can't really explain this either, the menu and pages are defined in an xml file). I don't understand how to specify the second action in my jsp, and how to make sure that the action is called when first loading the jsp. I would prefer a solution not involving AJAX, if possible. Thanks.

Imbue answered 4/8, 2011 at 15:56 Comment(0)
A
8

Why do you want to go for AJAX, when you have the power of Struts. I have a simple example (it is tested) for you.

MyForm.java

    package com.tusar.action;

    import java.io.Serializable;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import javax.servlet.http.HttpServletRequest;

    public class MyForm extends ActionForm implements Serializable{

        private static final long serialVersionUID = 1043346271910809710L;
        private String fullName = null;

        public String getFullName() {
          return fullName;
        }

        public void setFullName(String fullName) {
            this.fullName = fullName;
        }

        /*This method will be called when you press the reset button
              or load the form. You may want to populate the form here also*/

        public void reset(ActionMapping mapping, HttpServletRequest request){
        String reset = (String)request.getAttribute("myForm.reset");
            if ((null != reset)|| ("true".equals(reset))) {
                fullName = null;
            }
        }
    }

MyFormSetupAction.java

    package com.tusar.action;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    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 class MyFormSetupAction extends Action{

        /*Set your form-bean properties here*/

        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
            MyForm hwForm = (MyForm) form;
            hwForm.setFullName("tusar");
            return mapping.findForward("success");
        }

    }

MyFormSuccessAction.java

package com.tusar.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 class MyFormSuccessAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("success");
    }

}

struts-config.xml

    <?xml version="1.0" encoding="ISO-8859-1" ?>

    <!DOCTYPE struts-config PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
      "http://struts.apache.org/dtds/struts-config_1_3.dtd">

     <struts-config>

     <!-- ==================== Form Bean Definitions -->

     <form-beans>
     <form-bean name="myForm" type="com.tusar.action.MyForm">
     <form-property name="fullName" type="java.lang.String"/>
     </form-bean>

     <!-- ============= Global Forward Definitions -->

     <global-forwards>
     <!-- Default forward to "Welcome" action -->
     <!-- Demonstrates using index.jsp to forward -->
     <forward name="home" path="/home.do"/>
     </global-forwards>

     <!-- ===================== Action Mapping Definitions -->

     <action-mappings>

     <!-- This action will load the form-->

     <action path="/home"
        type="com.tusar.action.MyFormSetupAction"
        name="myForm"
        validate="false"
        input="/WEB-INF/jsp/home.jsp">

        <forward name="success" path="/WEB-INF/jsp/home.jsp" />
     </action>   

     <!-- This action will evalutae the form and pass form data to 
          success page-->

     <action path="/successAction" 
         type="com.tusar.action.MyFormSuccessAction" 
         name="myForm"
         validate="true"
         input="/WEB-INF/jsp/home.jsp">

        <forward name="success" path="/WEB-INF/jsp/success.jsp" />
     </action>

     </action-mappings>

         <!-- ============= Message Resources Definitions -->

     <message-resources parameter="MessageResources" />

     </struts-config>

home.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%> 
<html>
<head>
<title>Struts 1</title>
</head>
<body>

    <html:form action="/successAction.do">

    Name:   
    <html:text property="fullName"></html:text>

    <html:submit value="Next"></html:submit>

    <html:reset value="Cancel"></html:reset>

</html:form>

</body>
</html>

success.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<html>
<head>
<title>Successful !</title>
</head>
<body>
    <h3>Your details:</h3>

    Name:

    <bean:write name="myForm" property="fullName" />

</body>
</html>

Everytime you call the home.do action, the fullName property is populated with "tusar". Just comment if you need any further association, I'll be happy to help you. Thanks!

Afreet answered 29/8, 2011 at 6:42 Comment(4)
This code reminds me why people moved away from Struts 1 as soon as they had the chance--what a nightmare, just to get a form with a user's name :(Exurbanite
@tusar: There is an issue with a multipage form on Firefox. Clicking on back button repopulate the form without considering the changes done by the user (in edit mode).Fluor
@Cricket, hi! I think your are running into that problem because of caching in browsers. There are hacks available to disable caching in browsers (setting response headers for cache e.t.c). Please post your question as a separate thread.Afreet
@tusar: I set response header with no-store, but in this way the form is emptied. I don't understand because the last user entered values aren't cached. I'll put a question. Thanks.Fluor
E
0

There should be an action called when selecting the user. The properties of that user should be copied into the action form before returning the action forward.

Without any configuration information it's difficult to help beyond that.

Exurbanite answered 8/9, 2011 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.