Hi I'm trying to maintain my login session in a Struts 2 proyect. Specificly I want prohibit someone from entering a url thats only accessible if you are previously loged in.
This is my struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="LoginAction" class="com.mmedus.Action.OnlyAction"method="loginMethod">
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="RegisterAction" class="com.mmedus.Action.OnlyAction" method="registerMethod">
<result name="success">/successUser.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="CreateMatchAction" class="com.mmedus.Action.OnlyAction" method="createMatchMethod">
<result name="success">/successMatch.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="DisplayAllMatchAction" class="com.mmedus.Action.DisplayAllMatchAction" method="execute">
<result name="success">/displayAllMatch.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="DisplayMatch" class="com.mmedus.Action.DisplayMatchAction" method="execute">
<result name="success">/displayMatch.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="AddPlayer" class="com.mmedus.Action.DisplayMatchAction" method="addPlayer">
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
My OnlyAction class that validates my login
package com.mmedus.Action;
import com.opensymphony.xwork2.Action;
import com.mmedus.Form.*;
import com.mmedus.Model.*;
public class OnlyAction{
private Form form = new Form();
private User user = new User();
private Match match = new Match();
private MatchForm matchForm= new MatchForm();
public Form getForm() {
return form;
}
public void setForm(Form form) {
this.form = form;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Match getMatch() {
return match;
}
public void setMatch(Match match) {
this.match = match;
}
public MatchForm getMatchForm() {
return matchForm;
}
public void setMatchForm(MatchForm matchForm) {
this.matchForm = matchForm;
}
public String loginMethod(){
String status = Action.ERROR;
getUser().setUsername(getForm().getUsername());
getUser().setPassword(getForm().getPassword());
if(getForm().getService().getDao().getUser(getUser())){
status = Action.SUCCESS;
}else{
status = Action.ERROR;
}
return status;
}
public String registerMethod(){
String status = "error";
getUser().setUid(getForm().getUid());
getUser().setUname(getForm().getUname());
getUser().setUsername(getForm().getUsername());
getUser().setPassword(getForm().getPassword());
getUser().setAddress(getForm().getAddress());
if(getForm().getService().getDao().addUser(getUser())){
status = Action.SUCCESS;
}else{
status = Action.ERROR;
}
return status;
}
public String createMatchMethod(){
String status = "error";
getMatch().setName(getMatchForm().getName());
getMatch().setNumPlayers(getMatchForm().getNumPlayers());
getMatch().setLocation(getMatchForm().getLocation());
if(getMatchForm().getService().getDao().addMatch(getMatch()) || getMatch().getName()=="" || getMatch().getNumPlayers()==0 || getMatch().getLocation()==""){
status = Action.SUCCESS;
}else{
status = Action.ERROR;
}
return status;
}
/*
public String displayMatchMethod(){
String status = "error";
getMatch().setName(getMatchForm().getName());
getMatch().setNumPlayers(getMatchForm().getNumPlayers());
getMatch().setLocation(getMatchForm().getLocation());
if(getMatchForm().getService().getDao().getMatch(getMatch())){
status = Action.SUCCESS;
}else{
status = Action.ERROR;
}
return status;
}
*/
}
Can someone help me please? Let me know if there is more code needed. Thanks.
Aucun commentaire:
Enregistrer un commentaire