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]
# 1

What's the error you get?

Manuel Leiria

manuel.leiriaa at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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

RahulSharnaa at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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>

kayeanna at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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" />

RahulSharnaa at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

hmmm.. the result is still "Welcome: " without the username..

btw, the

<form-bean name="LoginActionForm" type="com.myapp.struts.LoginActionForm"/>

in the struts-config is auto-generated by the NetBeans..

i don't even know what a bean is? and the value to be placed in the name and property of <bean:write name="LoginActionForm" property="username"></bean:write>

sorry..

kayeanna at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

<action input="/welcomeStruts.jsp" name="LoginActionForm" path="/Login" scope="session" type="com.myapp.struts.LoginAction">

<forward name="success" path="/index.jsp"/>

you named it as LoginActionForm therefore <bean:write name="LoginActionForm" value="username" /> should work...

make sure you take out reset() statement out of your Action class code...

Just have a look at the code for LoginAction i stated...

RahulSharnaa at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

yes, i followed it already =) but it still doesn't display the username..

kayeanna at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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" />

vinayak_ra at 2007-7-29 12:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...