Simple Struts
Can anyone explain to me why the code isn't working?
Flow:
* User will input this details in the welcomeStruts.jsp:
Username: kayeann
Password: something
* System will call LoginActionForm then LoginAction then assuming it is successful, forwards it to index.jsp
* Expected result: Welcome kayeann!
index.jsp:
Welcome: <bean:write name="user" property="username"></bean:write>
welcomeStruts.jsp:
Username:<html:text property="username"></html:text>
Password:<html:password property="password"></html:password>
LoginActionForm.java:
private String username, password;
methods getUsername, getPassword, setUsername, setPassword
User.java:
private String username, password;
methods getUsername, getPassword, setUsername, setPassword
LoginAction.java:
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
User myUser = new User();
LoginActionForm myLoginActionForm = (LoginActionForm) form;
myUser.setUsername(myLoginActionForm.getUsername());
myUser.setPassword(myLoginActionForm.getPassword());
if (myUser.isValid ())
{
request.getSession().setAttribute("user", myUser); // persisting the object
myLoginActionForm.reset(mapping,request);
return mapping.findForward(Constants.LOGINSUCCESS);
}
else
{
myLoginActionForm.reset(mapping,request);
ActionErrors errors = new ActionErrors();
errors.add("login", new ActionMessage("error.login.mismatch"));
saveErrors(request.getSession(), errors);
return mapping.findForward(Constants.LOGINFAILURE);
}
}
[1806 byte] By [
kayeanna] at [2007-11-27 10:58:11]

# 2
well brother have you named your Form-Bean as "user" in ur struts-config.xml i.e one specific to the class LoginActionForm.
iF your answer is no and if you have named it to something else .
use.
and please don't reset(enforce) the Form Bean which you were doing in the action class b4 as it would be called by default
<bean:write name="formBeanName" property="username"/>
and if you are redirecting the login page make sure formbean resides within the scope of session.
Hope that might help :)
if it still doesn't it'd be gr8 if you can post your action-mappings specific to the described action.
REGARDS,
RaHuL
# 3
The result is> Welcome:
(The username does not print..)
here's my struts-config.xml:
<form-beans>
<form-bean name="LoginActionForm" type="com.myapp.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action input="/welcomeStruts.jsp" name="LoginActionForm" path="/Login" scope="session" type="com.myapp.struts.LoginAction">
<forward name="success" path="/index.jsp"/>
<forward name="failed" path="/welcomeStruts.jsp"/>
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
# 4
alright the one below shud help u
LoginAction.java:
==============
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
User myUser = new User();
LoginActionForm myLoginActionForm = (LoginActionForm) form;
myUser.setUsername(myLoginActionForm.getUsername());
myUser.setPassword(myLoginActionForm.getPassword());
if (myUser.isValid ())
{
request.getSession().setAttribute("user", myUser); // persisting the object
/*Don't manually enforce reset manually*/
return mapping.findForward(Constants.LOGINSUCCESS);
}
else
{
myLoginActionForm.reset(mapping,request);
ActionErrors errors = new ActionErrors();
errors.add("login", new ActionMessage("error.login.mismatch"));
saveErrors(request.getSession(), errors);
return mapping.findForward(Constants.LOGINFAILURE);
}
}
Index.jsp:
========
Welcome <bean:write name="LoginActionForm" value="username" />
# 8
The one below should help u
LoginAction.java:
==============
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
//User myUser = new User();
LoginActionForm myLoginActionForm = (LoginActionForm) form;
//myUser.setUsername(myLoginActionForm.getUsername());
//myUser.setPassword(myLoginActionForm.getPassword());
if (myLoginActionForm .isValid ()) // impl isValid method in LoginActionForm
{
// request.getSession().setAttribute("user", myUser); // persisting the object
/*Don't manually enforce reset manually*/
return mapping.findForward(Constants.LOGINSUCCESS);
}
else
{
myLoginActionForm.reset(mapping,request);
ActionErrors errors = new ActionErrors();
errors.add("login", new ActionMessage("error.login.mismatch"));
saveErrors(request.getSession(), errors);
return mapping.findForward(Constants.LOGINFAILURE);
}
}
Index.jsp:
========
Welcome <bean:write name="LoginActionForm" value="username" />