You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Valéry Jacot <pt...@hotmail.com> on 2002/09/24 15:33:01 UTC

Form reload problem


Hi...

I've the following problem:

I have a form tag in a jsp page, on submit, it validate the form and then
call the execute method of the action. If there is no errors in the validate
method of the form, no problem. But if there is some errors, it come back to
the page but don't write in the "<html:text>" tags the values that were
posted.
This works if, in the file struts-config.xml, I set the attribute scope of
the form in the action to "session". But I absolutly need to set it to
request.

So, what is my problem ???

Tanks...


struts-config.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!-- edited with XML Spy v4.4 U (http://www.xmlspy.com)
     This is the Struts configuration file for the example application,
     using the proposed new syntax.

     NOTE:  You would only flesh out the details in the "form-bean"
     declarations if you had a generator tool that used them to create
     the corresponding Java classes for you.  Otherwise, you would
     need only the "form-bean" element itself, with the corresponding
     "name" and "type" attributes.
--><struts-config>
	<!--========== FORM BEANS DEFINITIONS  ==========-->
	<form-beans>
		<form-bean name="loginForm"
type="com.eim.application.teststruts.form.LoginForm">
			<form-property name="firstName" type="java.lang.String" />
			<form-property name="lastName" type="java.lang.String" />
			<form-property name="password" type="java.lang.String" />
		</form-bean>
	</form-beans>
	<!--========== GLOBAL FORWARD DEFINITIONS  ==========-->
    <global-forwards>
      <forward name="login" path="/jsp/login.jsp" redirect="false" />
    </global-forwards>
	<!--========== ACTION MAPPING DEFINITIONS  ==========-->
	<action-mappings>
		<action path="/login" scope="request" validate="true" name="loginForm"
type="com.eim.application.teststruts.action.ActionLogin" unknown="false"
input="/jsp/login.jsp">


			<forward name="success" path="/jsp/home.jsp" redirect="true" />
			<forward name="failure" path="http://www.google.com" redirect="true" />
		</action>
	</action-mappings>
	<!--========== CONTROLLER CONFIGURATION  ==========-->
    <controller debug="3" inputForward="false" />
	<!--========== MESSAGE RESSOURCE DEFINITIONS  ==========-->
	<message-resources parameter="com.eim.application.teststruts.application">
		<!-- Define application properties files that are to use -->
	</message-resources>
	<!--========== PLUG-IN DEFINITIONS  ==========-->
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames" value="/WEB-INF/validator-rules.xml" />
	</plug-in>
	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		<set-property property="definitions-config"
value="/WEB-INF/def_config.xml" />
		<set-property property="definitions-debug" value="1" />
		<set-property property="definitions-parser-details" value="0" />
		<set-property property="definitions-parser-validate" value="true" />
	</plug-in>
</struts-config>


Code of the jsp page:

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>


<tiles:insert definition="layout.login" flush="true">
	<tiles:put name="title" value="login.title"/>
</tiles:insert>
<html:html>

<div style="position: absolute; top:80px; left: 0px; width: 100%;
text-align: center;">
	<html:errors/>
	<html:form action="/login" method="post" name="loginForm"
type="com.eim.application.teststruts.form.LoginForm">
	<table width="500" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td width="50%">First name</td>
			<td width="50%"><html:text property="firstName"/></td>
		</tr>
		<tr>
			<td width="50%">Last name</td>
			<td width="50%"><html:text property="lastName"/></td>
		</tr>
		<tr>
			<td width="50%">Password</td>
			<td width="50%"><html:password property="password"/></td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2"><html:submit value="Validate"/></td>
		</tr>
	</table>
	</html:form>
</div>
</html:html>

</html>


Code of the action form:

package com.eim.application.teststruts.form;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2002
* Company:
*
* @author
* @version    1.0
*/

public class LoginForm extends ActionForm {

	private String firstName = null;
	private String lastName = null;
	private String password = null;



	/**
	 *  Sets the firstName attribute of the LoginForm object
	 *
	 * @param  firstName  The new firstName value
	 */
	public void setFirstName( String firstName ) {
		this.firstName = firstName;
	}


	/**
	 *  Sets the lastName attribute of the LoginForm object
	 *
	 * @param  lastName  The new lastName value
	 */
	public void setLastName( String lastName ) {
		this.lastName = lastName;
	}


	/**
	 *  Sets the password attribute of the LoginForm object
	 *
	 * @param  password  The new password value
	 */
	public void setPassword( String password ) {
		this.password = password;
	}


	/**
	 *  Gets the firstName attribute of the LoginForm object
	 *
	 * @return    The firstName value
	 */
	public String getFirstName() {
		return this.firstName;
	}


	/**
	 *  Gets the lastName attribute of the LoginForm object
	 *
	 * @return    The lastName value
	 */
	public String getLastName() {
		return this.lastName;
	}


	/**
	 *  Gets the password attribute of the LoginForm object
	 *
	 * @return    The password value
	 */
	public String getPassword() {
		return this.password;
	}


	/**
	 *  Description of the Method
	 *
	 * @param  mapping  Description of the Parameter
	 * @param  request  Description of the Parameter
	 */
	public void reset( ActionMapping mapping, HttpServletRequest request ) {
		this.password = null;
		this.firstName = null;
		this.lastName = null;
	}


	/**
	 *  Description of the Method
	 *
	 * @param  mapping  Description of the Parameter
	 * @param  request  Description of the Parameter
	 * @return          Description of the Return Value
	 */
	public ActionErrors validate( ActionMapping mapping,
	                              HttpServletRequest request ) {

		ActionErrors errors = new ActionErrors();
		if ( ( firstName == null ) || ( firstName.length() < 1 ) ) {
			errors.add( "firstName", new ActionError( "error.firstName.required" ) );
		}
		if ( ( lastName == null ) || ( lastName.length() < 1 ) ) {
			errors.add( "lastName", new ActionError( "error.lastName.required" ) );
		}
		if ( ( password == null ) || ( password.length() < 1 ) ) {
			errors.add( "password", new ActionError( "error.password.required" ) );
		}
		return errors;
	}
}


code of my action:

package com.eim.application.teststruts.action;

import java.util.Locale;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.Action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.AppException;
import org.apache.struts.util.MessageResources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.eim.application.teststruts.model.User;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2002
* Company:
*
* @author
* @version    1.0
*/

public class ActionLogin extends Action {

	/**
	 *  Gets the user attribute of the LoginAction object
	 *
	 * @param  form           Description of the Parameter
	 * @return                The user value
	 * @exception  Exception  Description of the Exception
	 */
	public User getUser( ActionForm form )
		throws Exception {
		String firstName = (String) PropertyUtils.getSimpleProperty( form,
"firstName" );
		String lastName = (String) PropertyUtils.getSimpleProperty( form,
"lastName" );
		String password = (String) PropertyUtils.getSimpleProperty( form,
"password" );
		return new User( firstName, lastName, password );
	}


	/**
	 *  Description of the Method
	 *
	 * @param  mapping        Description of the Parameter
	 * @param  form           Description of the Parameter
	 * @param  request        Description of the Parameter
	 * @param  response       Description of the Parameter
	 * @return                Description of the Return Value
	 * @exception  Exception  Description of the Exception
	 */
	public ActionForward execute( ActionMapping mapping,
	                              ActionForm form,
	                              HttpServletRequest request,
	                              HttpServletResponse response )
		throws Exception {

		User currentUser = this.getUser( form );
		HttpSession session = request.getSession();
		session.setAttribute( "currentUser", currentUser );

		// Forward control to the specified success URI
		return ( mapping.findForward( "success" ) );
	}
}


Thanks...

See you

Valéry Jacot

PS: Sorry for my english, I'm a frenchy...





_________________________________________________________________
Affichez, modifiez et partagez gratuitement vos photos en ligne: 
http://photos.msn.com/support/worldwide.aspx


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>