I am new to struts, there might be better ways to make this work.
You can maintain state information with session registery or http
parameters..
A working example:
----------------struts-config.xml
---------------------------------------
<form-beans>
<form-bean name="loginForm" type="com.trend.forms.loginForm"/>
</form-beans>
<action path="/login"
input="/chatwalk.jsp"
type="com.trend.actions.loginAction"
name="loginForm"
scope="request"
validate="true">
<forward name="exception" path="/exception.jsp" />
<forward name="success" path="/myChatwalk.jsp"/>
<forward name="failure"
path="/chatwalk.jsp?auth=invalid&nav=main"/>
</action>
-----------------loginAction.java -------------------------------------
public final class loginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
CWUtil util = new CWUtil();
CWDAO service = new CWDAO();
HttpSession session = request.getSession();
String auth = null;
ActionForward ret = null;
userBean user = new userBean();
loginForm loginForm = (loginForm) form;
loginBean login = new loginBean();
BeanUtils.copyProperties( login, loginForm );
try{
auth = service.login(login);
if(!auth.startsWith("Ex")){
user = util.parseLogin(auth);
session.setAttribute("user",user);
ret = mapping.findForward("success");
}else{
ret = mapping.findForward("failure");
}
}catch(Exception e){
System.out.println("exception at login action " +
e.getMessage());
ret = mapping.findForward("exception");
}
return ret;
}
}
---------------------------- loginForm.java
--------------------------------
package com.trend.forms;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class loginForm extends ActionForm {
String username,password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public ActionErrors validate( ActionMapping mapping,
HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
HttpSession session = request.getSession();
if ( getUsername() == null || getUsername().trim().length() == 0
) {
errors.add("username",new
ActionMessage("errors.required","username"));
session.setAttribute("loginError","true");
}if ( getPassword() == null || getPassword().trim().length() ==
0 ) {
errors.add("password",new
ActionMessage("errors.required","password"));
session.setAttribute("loginError","true");
}
return errors;
}
}
-----------------------------------loginBean.java-----------------------
----
package com.trend.beans;
public class loginBean {
String username,password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
-----Original Message-----
From: siva [mailto:[EMAIL PROTECTED]
Sent: Monday, February 13, 2006 12:06 PM
To: [email protected]
Subject: Passing Parameters to ActionForward from Action
Hi,
I want to know how to pass parameters from an Action to corresponding
ActionForward.
Basically, I am having only the jsp name in forward in
struts-config.xml.
But, when action is processed, I want to send some parameters to the
corresponding actionforward.
Can some be help me in explaining how it can be achieved.
Thanks,
Siva
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]