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.
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!