3

我的代码有各种问题:

  1. 我无法查看我ActionMessagesActionErrors设置LoginAction
  2. 登录后,如果我再次转到 URL: /login,它不会将我重定向到成功页面。

编辑:

  1. 如果会话已经存在,我现在可以成功重定向。我必须做出2个我想出的改变:
  • 首先,在我的动作类中,我设置void setSession(Map map)了错误的会话变量(它应该只是一个 setter 函数)。
  • 其次,我检查了validate()解决问题的方法中是否存在会话变量。
  1. 但我仍然无法查看我的ActonErrorsand ActionMessages

  2. 是否有必要使用<s:form>. 因为它扰乱了我的 CSS,我无法找到解决方案来纠正它。我可以在中分配 ID 属性<s:form>吗?

以下是我的 JSP 代码:

...
        <s:if test="hasActionErrors()">
            <div id="errorMessage"><s:actionerror /></div>
        </s:if>
        <s:if test="hasActionMessages()">
            <div id="errorMessage"> <s:actionmessage /></div>
        </s:if>
        <form action="login" method="POST" id="login-form">
            <fieldset>
                <p>
                    <label for="loginUserName">username</label>
                    <input type="text" id="loginUserName" maxlength="10" placeholder="Eg. EMP0000000" 
                    title="User ID is of format EMP0000000" pattern="(EMP)([0-9]{7})" required
                     name="loginUserName" class="round full-width-input" autofocus />
                </p>

                <p>
                    <label for="loginPassword">password</label>
                    <input type="password" name="loginPassword" maxlength="20" id="loginPassword" required 
                    class="round full-width-input" />
                </p>
                
                <p>I've <a href="#">forgotten my password</a>.</p>
                
                <input type="submit" class="button round blue image-right ic-right-arrow" value="LOG IN" />

            </fieldset>

        </form>
...

这是我的struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
        
<struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="false" />

        <package name="employee" extends="struts-default" namespace="/">

            <interceptors>
                <interceptor name="login" class="com.sreebhog.interceptors.LoginInterceptor" />
        
                <interceptor-stack name="loginStack">
                    <interceptor-ref name="login" ></interceptor-ref>
                    <interceptor-ref name="defaultStack" ></interceptor-ref>
                </interceptor-stack>
            </interceptors>
                <default-interceptor-ref name="loginStack"></default-interceptor-ref>
            <global-results>
                <result name="loginRedirect">/login </result>
            </global-results>
        
            <action name="login" class="com.sreebhog.actions.LoginAction">
                <result name="input">/JSP/sreebhog_login.jsp</result>
                <result name="success">/JSP/index.jsp</result>
            </action>
            <action name="logout" class="com.sreebhog.actions.LogoutAction">
                <result name="logout">/JSP/sreebhog_login.jsp</result>
            </action>
        </package>


</struts>

这是我的LoginAction

package com.sreebhog.actions;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.sreebhog.dao.EmployeeDAO;
import com.sreebhog.interfaces.LoginRequired;
import com.sreebhog.model.dto.Employee;
import com.sreebhog.utility.Cryptography;

public class LoginAction extends ActionSupport implements SessionAware,LoginRequired {
    
    private String loginUserName;
    private String loginPassword;
    
    SessionMap<String,Object> sessionMap;
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void setSession(Map map) {
        sessionMap=(SessionMap) map;
        sessionMap.put("username", loginUserName);
    }
        
    public String execute()
    {
        String userName = null;
          if (sessionMap.containsKey("username"))
          { 
              userName = (String) sessionMap.get("username");
              if (userName != null )
              {
                  if (!userName.equals(""))
                  {
                      return SUCCESS;
                  }
              }
          }
          
        EmployeeDAO employeeDao = new EmployeeDAO();
        Employee employee = employeeDao.find(this.getLoginUserName());
        if (employee == null || employee.getUserName().equals(""))
        {
            System.out.println("Here1");
            addActionError("Username or password is invalid");
            return INPUT;
        } else
            try {
                if (employee.getPassword().equals(Cryptography.getHash("SHA-1", this.getLoginPassword(), employee.getSalt())))
                {
                    System.out.println("Here2");
                    addActionError("Username or password is invalid");
                    return INPUT;
                }
                else {
                    System.out.println("Here3");
                    sessionMap.put("username",(String)loginUserName);
                    sessionMap.put("role",(String)employee.getPermissions().getRole());
                    return SUCCESS;
                }
            } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        return INPUT;
    }
    
    public void validate()
    {
        
        if (loginUserName == null || loginUserName.equals("") || loginPassword == null || loginPassword.equals(""))
        {
            addActionError("Please fill in username and password");
        }
    }

    public String logout()
    {
        sessionMap.invalidate();
        return INPUT;
    }

    public String getLoginUserName() {
        return loginUserName;
    }

    public void setLoginUserName(String loginUserName) {
        this.loginUserName = loginUserName;
    }

    public String getLoginPassword() {
        return loginPassword;
    }

    public void setLoginPassword(String loginpassword) {
        this.loginPassword = loginpassword;
    }

}

这是我的LoginInterceptor

package com.sreebhog.interceptors;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.sreebhog.actions.LoginAction;
import com.sreebhog.interfaces.LoginRequired;

public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();
        String userName = (String) session.get("username");

        if (userName != null && !userName.equals("")) {
            return invocation.invoke();
        }

        Object action = invocation.getAction();

        if (!(action instanceof LoginRequired)) {
            return invocation.invoke();
        }

        if (!(action instanceof LoginAction)) {
            return "loginRedirect";
        }

        return invocation.invoke();
    }
}
4

3 回答 3

1

问题解决了。我不敢相信我做到了。忘记在我的 jsp 页面中放置 tag-lib。感谢大家的帮助。

于 2013-04-20T10:11:45.147 回答
0

将错误消息标签和消息标签放入表单中。如下所示

<s:form>
            <div id="errorMessage"><s:actionerror /></div>
            <div id="errorMessage"> <s:actionmessage /></div>

.
.
.
</s:form>
于 2013-02-27T17:43:51.467 回答
0
  1. 您无法查看操作消息,因为您没有通过addActionMessage. 您应该看到的操作错误。还要检查!loginUserName.equals("null").

  2. 登录并请求/login检查拦截器中的会话对象username后,它不应为空,然后login调用操作。但是参数是空的,只有在提交表单时才可用。验证应该失败并返回到input. 也不要在username注入会话时将其放到地图上,在 return 之前做success

于 2013-02-27T18:46:47.930 回答