0

My index.jsp is

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 <head>
<title>HOME</title>
</head>

<body>
Hi you are in index.jsp page
<s:action name="error" executeResult="true"></s:action>
</body>
</html>

my error action class is

public class error extends ActionSupport {
  public String execute() throws Exception {
    System.out.println("in error action.........");
    return SUCCESS;
  }
}

my struts.xml

<action name="error" class="action.error">            
        <result name="success" type="redirect">                                                      
            <param name="location">/Login.jsp</param>
        </result>            
</action>

i want to redirect to Login.jsp when i hit the index.jsp page. but every time i hit index.jsp i come back to my index.jsp page but this time it is blank. it is going to action as it is printing "in error action.........". But it is not redirecting to Login.jsp

what am i doing wrong?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
anonymous
  • 244
  • 2
  • 4
  • 23

1 Answers1

1

what am i doing wrong?

Basically everything.

  1. Class names must start with a capitalized letter, according to Java Naming Conventions:

    public class Error extends ActionSupport {
    
  2. Naming error an action returning login data (or page) is a big violation of the POLA; a name like login fits a lot better;

  3. redirect result must point to an URL, generally external (eg. http://www.google.com/). To redirect to an action, use redirectAction result, to point to a view (eg. a JSP) use the default result, dispatcher (that can be omitted).

    <action name="login" class="action.Login">  
        <result name="success">/Login.jsp</result>
    </action>
    
  4. Using <s:action> tag is 99% of times unnecessary, and sometimes also wrong, like in this case, because the result execution will be injected in the main page. You could inject a JSP snippet containing a Javascript block with a window.location.href = something, but this is not the Circus;
    redirect the right way, by either using the meta refresh, or (much better) by not even landing on the index.jsp, redirecting directly from the Index.action (with a redirectAction result) to the Login.action, and then landing on login.jsp.

    Otherwise, you could use a small Scriptlet like this:

    <% response.sendRedirect("/login.action"); %> 
    

    but not from included JSPs, only from top level.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243