Hi
All,
I am currently
experiencing a problem in regards to redisplaying form data a user has entered
within a form.
I am experiencing
this problem both from ActionForm and LookupDispatchAction classes in a
major project.
As a consequence I
decided to
write a very small login application to see if I could solve this
problem.
The
login application will display a user login application screen. If a user
tries to login, and does so successfully a
welcome page is displayed. However if
not successful a informative message should appear, as well as what the user has
entered into the fields.
Unfortunately the
data the the user has enetered never gets repopulated in the form text fields
when login has been unsuccessful.
Has anyone got a
solution to this problem. Any help would be appreciated.
Cheers
Rodney
ps. NOTE: The first
screen the user sees comes directly from a JSP page (not from a redirect of a
Action class to a JSP page).
pss. The code is
shown below and is attached to this email.
Struts-Config.xml
--------------------------
<struts-config>
<form-beans>
<form-bean name="test.LoginActionForm" type="net.natdata.application.test.login.LoginActionForm" />
</form-beans>
<action-mappings>
<action path="/LoginAction"
type="net.natdata.application.test.login.LoginAction"
name="test.LoginActionForm"
input="/Login.jsp"
scope="request"
validate="true"
parameter="method">
<forward name="failure" path="/Login.jsp"/>
<forward name="success" path="/LoginSuccessful.jsp"/>
</action>
</action-mappings>
<message-resources parameter="resources.application" />
</struts-config>
<form-beans>
<form-bean name="test.LoginActionForm" type="net.natdata.application.test.login.LoginActionForm" />
</form-beans>
<action-mappings>
<action path="/LoginAction"
type="net.natdata.application.test.login.LoginAction"
name="test.LoginActionForm"
input="/Login.jsp"
scope="request"
validate="true"
parameter="method">
<forward name="failure" path="/Login.jsp"/>
<forward name="success" path="/LoginSuccessful.jsp"/>
</action>
</action-mappings>
<message-resources parameter="resources.application" />
</struts-config>
Application
Resources (Property file)
-----------------------------------------------------
button.login=Login
field.userIdentifier=User ID
field.password=Password
field.userIdentifier=User ID
field.password=Password
heading.login=Login
error.generalMessage=An error has occured and all processing has been
suspended until further notice.
error.invalidPassword=The password you have entered is incorrect.
error.invalidUser=The user entered doesnt exist.
error.invalidPassword=The password you have entered is incorrect.
error.invalidUser=The user entered doesnt exist.
error.userIdentifier.required=A user id must be entered to log
in.
error.password.required=A password must be entered to log in.
error.password.required=A password must be entered to log in.
Login.jsp
-------------
<%@ page
language="java" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html>
<head>
<title><bean:message key="heading.login" /></title>
</head>
<body>
<center><h1><bean:message key="heading.login" /></h1></center>
<center><html:errors /></center>
<html:form action=""
name="test.LoginAction"
type="net.natdata.application.test.login.LoginActionForm">
<table align="center" border="0" cellpadding="2" cellspacing="0">
<tr>
<td><bean:message key="field.userIdentifier" />:</td>
<td><html:text property="userIdentifier" /></td>
</tr>
<tr>
<td><bean:message key="field.password" />:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="right">
<html:submit property="method">
<bean:message key="button.login" />
</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html>
<head>
<title><bean:message key="heading.login" /></title>
</head>
<body>
<center><h1><bean:message key="heading.login" /></h1></center>
<center><html:errors /></center>
<html:form action=""
name="test.LoginAction"
type="net.natdata.application.test.login.LoginActionForm">
<table align="center" border="0" cellpadding="2" cellspacing="0">
<tr>
<td><bean:message key="field.userIdentifier" />:</td>
<td><html:text property="userIdentifier" /></td>
</tr>
<tr>
<td><bean:message key="field.password" />:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="right">
<html:submit property="method">
<bean:message key="button.login" />
</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html>
LoginSuccessful.jsp
-----------------------------
<%@ page
language="java" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<center><h1>Login</h1></center>
<center>You have successfully logged into the system</center>
</body>
</html>
<head>
<title>Login</title>
</head>
<body>
<center><h1>Login</h1></center>
<center>You have successfully logged into the system</center>
</body>
</html>
LOGIN ActionForm
Class
-------------------------------------
public class
LoginActionForm extends ActionForm {
// Variables used within the form.
private String userIdentifier;
private String password;
private String method;
public void setUserIdentifier(String userIdentifier) {
this.userIdentifier = userIdentifier;
}
public String getUserIdentifier() {
return userIdentifier;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
ActionErrors errors = new ActionErrors();
boolean failure = false;
if(method.equals("Login")) {
if((userIdentifier == null) || (userIdentifier.trim().equals(""))) {
errors.add(userIdentifier, new ActionError("error.userIdentifier.required"));
}
else {
if((password == null) || (password.trim().equals(""))) {
errors.add(userIdentifier, new ActionError("error.password.required"));
}
}
}
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userIdentifier = null;
this.password = null;
this.method = null;
}
}
// Variables used within the form.
private String userIdentifier;
private String password;
private String method;
public void setUserIdentifier(String userIdentifier) {
this.userIdentifier = userIdentifier;
}
public String getUserIdentifier() {
return userIdentifier;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
ActionErrors errors = new ActionErrors();
boolean failure = false;
if(method.equals("Login")) {
if((userIdentifier == null) || (userIdentifier.trim().equals(""))) {
errors.add(userIdentifier, new ActionError("error.userIdentifier.required"));
}
else {
if((password == null) || (password.trim().equals(""))) {
errors.add(userIdentifier, new ActionError("error.password.required"));
}
}
}
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userIdentifier = null;
this.password = null;
this.method = null;
}
}
LOGIN Action
Class
-----------------------------
public class
LoginAction extends LookupDispatchAction {
protected java.util.Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.login", "login");
return map;
}
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = "success";
ActionErrors actionErrors = new ActionErrors();
try {
LoginActionForm loginForm = (LoginActionForm)form;
if(!loginForm.getUserIdentifier().equals("user")) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidUser"));
target = "failure";
}
else if(!loginForm.getPassword().equals("password")) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidPassword"));
target = "failure";
}
}
catch(Exception e) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.generalMessage"));
target = "failure";
}
finally {
if(!actionErrors.isEmpty()) {
saveErrors(request, actionErrors);
}
return (ActionForward)mapping.findForward(target);
}
}
}
protected java.util.Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.login", "login");
return map;
}
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = "success";
ActionErrors actionErrors = new ActionErrors();
try {
LoginActionForm loginForm = (LoginActionForm)form;
if(!loginForm.getUserIdentifier().equals("user")) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidUser"));
target = "failure";
}
else if(!loginForm.getPassword().equals("password")) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidPassword"));
target = "failure";
}
}
catch(Exception e) {
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.generalMessage"));
target = "failure";
}
finally {
if(!actionErrors.isEmpty()) {
saveErrors(request, actionErrors);
}
return (ActionForward)mapping.findForward(target);
}
}
}
<?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">
<struts-config> <form-beans> <form-bean name="test.LoginActionForm" type="net.natdata.application.test.login.LoginActionForm" /> </form-beans> <action-mappings> <action path="/LoginAction" type="net.natdata.application.test.login.LoginAction" name="test.LoginActionForm" input="/Login.jsp" scope="request" validate="true" parameter="method"> <forward name="failure" path="/Login.jsp"/> <forward name="success" path="/LoginSuccessful.jsp"/> </action> </action-mappings> <message-resources parameter="resources.application" /> </struts-config>
<?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> <display-name>Login Application</display-name> <!-- Servlet definitions section --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>resources.application</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>validate</param-name> <param-value>false</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Servlet Mapping section --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Session Config --> <session-config> <!-- 30 minutes --> <session-timeout>30</session-timeout> </session-config> <!-- Taglibs --> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/lib/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/lib/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/lib/struts-logic.tld</taglib-location> </taglib> </web-app>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]