My action classes would often have an action
field variable that was assigned a value inside the execute()
method:
public class MyAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String action;
@Override
public String execute() throws Exception {
action = "true";
// code here
return SUCCESS;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
In the JSP, I check whether the action
variable (which is now passed as request) is null
. If null
, it will redirect to the action class, else, it will continue to render the page:
<head>
<c:if test="${action == null}">
<c:redirect url="myaction" />
</c:if>
</head>
I do this to ensure the user passes through the action first in the event they try to illegally jump to the JSP.
It works as intended, but is there any other elegant way to do this?