ログインページを作成し、ログアウト後に前のページではなくログインページを表示させたい
ユーザーがログアウト後に前のページに戻らないようにする方法。キャッシュをクリアしました....しかし、戻るボタンを押すと、ユーザーは前のページに移動します.ログアウトした後、ユーザーが戻るボタンを押すと、ログインページが更新されて表示されます
<s:form action="Login" >
<s:textfield label="username" name="userName"/>
<s:password label="password" name="password"/>
<s:submit name="login" value="login"></s:submit>
</s:form>
セッションの管理方法も.javaにログインするのを手伝ってくれる人はいますか
package action;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private String userName;
private String password;
public Login() {
}
@Override
public String execute() {
Map session = ActionContext.getContext().getSession();
session.put("logged-in","yes");
return SUCCESS;
}
@Override
public void validate()
{
if(getUserName().length()==0)
{
addFieldError("userName", "User Name is required");
}
else if (!getUserName().equals("prerna"))
{
addFieldError("userName", "Invalid User");
}
if(getPassword().length()==0)
{
addFieldError("password", "password is required");
}
else if (!getPassword().equals("prerna")) {
addFieldError("password", getText("password.required"));
}
}
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
Logout.java
public class Logout {
public Logout() {
}
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return "success";
}
}
logout.jsp
<s:property value="userName"/>
<s:property value="password"/>
<s:url action="Logout.action" var="urlTag">
</s:url>
<s:a href="%{urlTag}">URL Tag Action (via %)</s:a>
インターセプターログインテスト
package interceptor;
import action.Login;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Map;
public class logintest implements Interceptor {
public logintest() {
}
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void init() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
Map<String, Object> session = ActionContext.getContext().getSession();
// sb: feel free to change this to some other type of an object which
// represents that the user is logged in. for this example, I am using
// an integer which would probably represent a primary key that I would
// look the user up by with Hibernate or some other mechanism.
String userId = (String) session.get("logged-in");
// sb: if the user is already signed-in, then let the request through.
if (userId != null) {
return actionInvocation.invoke();
}
Object action = actionInvocation.getAction();
// sb: if the action doesn't require sign-in, then let it through.
// sb: if this request does require login and the current action is
// not the login action, then redirect the user
if (!(action instanceof Login)) {
return "loginRedirect";
}
// sb: they either requested the login page or are submitting their
// login now, let it through
return actionInvocation.invoke();
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="logintest"
class="interceptor.logintest"></interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="logintest"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<global-results >
<result name="loginRedirect" type="redirect" >/login.jsp</result>
</global-results>
<action class="action.Login" name="Login">
<interceptor-ref name="newStack"></interceptor-ref>
<result name="input">/login.jsp</result>
<result name="success">/loginsuccess.jsp</result>
</action>
<action class="action.Logout" name="Logout">
<interceptor-ref name="newStack"></interceptor-ref>
<result name="success">/login.jsp</result>
</action>
</package>