Hi. I am developing a web app using
struts 1 and ajax together. However i am having some redirection issues.
I m pasting my code below :
struts-config.xml
<action
path="/login"
name="loginForm"
type="action.LoginAction"
parameter="method"
scope="request"
input="/login.jsp"
validate="false">
<forward name="success" path="/pages/my_home_page.jsp"/>
<forward name="failure" redirect="true" path="/pages/login.jsp"/>
</action>
and my login.js ( javascript file) ... this is where all my ajax code is present
$(function(){
//jAlert("hi");
$('#login_ok').click(function(){
$.ajax({
data:{username:$("#username").val(),
password:$("#password").val()},
type:"post",
url:"/Tourini/login.do",
success: function(response) { // on success..
},
failure: function(response){
jAlert("Error");
}
});
Action class :
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside function LoginAction:loginUser");
ActionForward af = null;
try{
DynaActionForm dform = (DynaActionForm)form;
Users users = new Users();
String username = ((String)dform.get("username")).trim();
String password = ((String)dform.get("password")).trim();
System.out.println("username "+username+" and password "+password);
users.setUsername(username);
users.setPassword(password);
LoginService ls = new LoginService();
String result = ls.loginUser(users);
System.out.println("result is "+result);
if(result.equals("success")){
af = mapping.findForward("success");
}else if(result.equals("fail")){
System.out.println("failure to login");
request.getSession().setAttribute("invalidUser", "true");
af = mapping.findForward("failure");
}
}catch(Exception e){
e.printStackTrace();
}
return af;
}
on successful login, it should be redirected to my_home_page.jsp but it comes on the login.jsp page itself.
Please suggest some hint.. i m not able to get this.