无法在Struts 2中成功重定向动作

时间:2016-12-25 14:18:52

标签: jsp struts2

我正在运行struts 2并且已经成功连接到我的SQL服务器,一切正常,除了我试图以不同方式重定向我的用户和管理员,就像有人登录网络时,我想将他重定向到homepage.jsp(如果他是普通用户)或control.jsp(如果他是该页面的管理员)并且我没有让这部分工作,我有以下代码但是用户和管理员都被重定向到管理页面。如果有人能告诉我我的错误,或者我错过了任何其他信息,那会很好。

struts.xml中

<!-- Login Function -->
<action name="Login" class="Actions.Login">
        <result type="redirectAction">
            <param name="actionName">LevelofAccess</param>
        </result>
        <result name="input">Index.jsp</result>
</action>
<action name="LevelofAccess" class="Actions.LevelofAccess">
        <result name="success">/WEB-INF/Control.jsp</result>
        <result name="input">/WEB-INF/Homepage.jsp</result>
        </action>

Login.java(不要认为错误在这里)

package Actions;
import Objects.ConnectSQL;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class Login extends ActionSupport implements SessionAware {
private Map session;

private String username = "";  
private String password = "";  


ConnectSQL connectSQL = new ConnectSQL();

public Login() {
}

@Override
public void validate(){
    // Check if username exists on session map.  If it exists, it means that user log in before
    Object usernameobject = session.get("username");  
    if (usernameobject == null) {
        //nothing will happen
    }
    else {
        // Username exists on session map, i.e. user has  already logged in.
        return;
    }

    // Connect to MS SQL Server database.
    String message = connectSQL.connect("localhost", 1433, "Online", "Eugene", "Eugene"); //Shopping = Databasename, Eugene = user, Eugene = pass
    if (message != null) { //if message null, means connection OK
        addActionError(message);
        return;
    }

    // Check if username and password are correct.
    boolean userfound = false; //Set to false as if it is found, will be set to true later
    ResultSet select = connectSQL.executeSQLStatement("SELECT * FROM Users"); //Choosing the value inside the table
    try { // looping to find the username
        while (select.next()) { 
            String selectusername = select.getString("Username"); //Field name in table <<
            String selectpassword = select.getString("Password");

            if (username.equals(selectusername)) {
                userfound = true;
                if (!password.equals(selectpassword)) {
                    addFieldError("password", "Incorrect password");
                    return;
                }
            }
        }
    }
    catch (Exception e) {
        addActionError(e.getMessage());
        return;
    }

    if (userfound) {
        // At this point, username and password are correct.
        // So we add the username to the session map to enable subsequent actions to be able
        // to check that user has already logged in.
        session.put("username", username);  
    }
    else
        addFieldError("username", "Invalid username");
}

@Override
public String execute() {
return SUCCESS;
}

@Override
public void setSession(Map session) {
    this.session = session;
}

public void setUsername(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.password = password;
}
}

LevelofAccess.java(1是我的“user”的sql数据,2是我的“admin”的sql数据)

ConnectSQL connectSQL = new ConnectSQL();

    String username;
public LevelofAccess(){
}


@Override
public void validate(){
Object usernameObject = session.get("username");
    if (usernameObject == null) {
        addActionError("Error: User is not found.  Please log in again.");
        return;
    }
    else
        username = (String) usernameObject;

    // Connect to MS SQL Server database.
    String message = connectSQL.connect("localhost", 1433, "Online", "Eugene", "Eugene");
    if (message != null) {
        addActionError(message);
    }

    ResultSet rs = connectSQL.executeSQLStatement("SELECT Type FROM Online.dbo.Users where Username = " + "'" + username + "'");
try{    
while(rs.first()){
    int gettype = rs.getInt("Type");
    if (gettype == 1){
    return;
    }
    else {
    // do nothing     
    }
    }
}
catch (Exception e) {
}
}

@Override
public String execute() {
    return SUCCESS;
}

@Override
public void setSession(Map session) {
    this.session = session;
}
}

0 个答案:

没有答案