代码:
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>
    <package name="login" extends="struts-default">
     <!-- 配置拦截器 -->    
        <action name="teacherlogin" class="cn.nit.action.TeacherAction">
        
         <!-- 结果配置信息 -->
        
            <result name="success" type="redirect">/teacher/index.jsp</result>
            <result name="error" type="redirect">/index.jsp</result>
            
            <!-- 应用拦截器 -->
           
        </action>
        
        <action name="adminlogin" class="cn.nit.action.AdminAction">
         <!-- 结果配置信息 -->
            <result name="success">/admin/index.jsp</result>
            <result name="error">/admin/adminLogin.jsp</result>
            
            <!-- 应用拦截器 -->
            
        </action>
    </package>
</struts>Action:package cn.nit.action;import java.util.ArrayList;
import java.util.Map;import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;import cn.nit.hibernate.Users;
import cn.nit.hibernateUtils.HibernateUtils;
import cn.nit.management.UserLogin;/**
 * 教师登录
 * @author huzg
 *
 */
public class TeacherAction implements SessionAware{

/**
 * 采用模型驱动方式
 */
UserLogin userLogin = new UserLogin();//这个类已经好,没问题

private Map session;//通过继承SessionAware得到session

public UserLogin getUser() {
return userLogin;
} public void setUser(UserLogin userLogin) {
this.userLogin = userLogin;
}

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

public String execute(){

this.session.put("username", userLogin.getUsername());//将用户放入到session中

System.out.println("验证码为: " + this.session.get("rand"));
System.out.println("用户输入的验证为: " + userLogin.getValidate());
System.out.println("用户输入的用户名为: " + userLogin.getUsername());
System.out.println("用户输入的密码为: " + userLogin.getPassword());

String username = userLogin.getUsername();
String password = userLogin.getPassword();

Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
String sql = " select u from Users as u where u.userNo= '" + username +"'";

Query query = session.createQuery(sql);

ArrayList userList = (ArrayList)query.list();

tx.commit();

HibernateUtils.close(session);

Users user = new Users();

if(!this.session.get("rand").equals(userLogin.getValidate())){
return "error";
}else if((userList !=null) && (userList.size()>0)){
user = (Users) userList.get(0);
System.out.println("user.userNO = " + user.getUserNo());
System.out.println("user.userPwd = " + user.getUserPwd());
if(!(user.getUserPwd().trim()).equals(password))
return "error";
}else{
return "error";
}
return "success";
}
}index.jsp:因为这个页面比较长,只是贴出了一点:<form id=Form1 action="teacher/teacherlogin.action" method="post">
   用户输入用户名,密码,验证码
</form
在没有写拦截器之前,可以成功登录,连接的是sql server 2005,但是在struts.xml文件添加如下代码:<!-- 配置拦截器 -->
     <interceptors>
     <interceptor name="sessionNull" class="cn.nit.interceptor.SessionNullInterceptor"/>
     </interceptors>       .....
        <!-- 应用拦截器 -->
            <interceptor-ref name="sessionNull"/>
            <interceptor-ref name="defaultStack"/>
cn.nit.interceptor.SessionNullInterceptor代码:package cn.nit.interceptor;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;/**
 * session为空的拦截器
 * @author Administrator
 *
 */
//拦截器实现Interceptor接口
public class SessionNullInterceptor implements Interceptor { public void destroy() {
// TODO Auto-generated method stub

} public void init() {
// TODO Auto-generated method stub

} public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
HttpServletRequest req = ServletActionContext.getRequest();
if (req.getSession().getAttribute("username") == null) {
return Action.LOGIN;//这个还没处理,只是暂时写个
} else {
return invocation.invoke();
} }

}下面是错误提示:HTTP Status 404 - No result defined for action cn.nit.action.TeacherAction and result login--------------------------------------------------------------------------------type Status reportmessage No result defined for action cn.nit.action.TeacherAction and result logindescription The requested resource (No result defined for action cn.nit.action.TeacherAction and result login) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.14
从上面的提示好像是说没有早到cn.nit.action.TeacherAction,但是我在没有写拦截器之前,是没问题,可以成功登录到用户页面,只要写了拦截器,就出现了那个错误?怎么回事?重复一下,我在没写拦截器之前是可以成功登录,所以struts.xml文件中的<action ...></action>是没问题的。

解决方案 »

  1.   

    感觉应该是struts.xml的配置写错了
    你能把strusts.xml里的东西发全吗?可能是默认的拦截器被屏蔽掉了
      

  2.   

    感觉应该是struts.xml的配置写错了 
      

  3.   


    “可能是默认的拦截器被屏蔽掉了”?我当时也是这么想的,但是我添加了;嗯,我把写了拦截器的strtus.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>
        <package name="login" extends="struts-default">
         <!-- 配置拦截器 -->
         <interceptors>
         <interceptor name="sessionNull" class="cn.nit.interceptor.SessionNullInterceptor"/>
         </interceptors>
        
            <action name="teacherlogin" class="cn.nit.action.TeacherAction">
            
             <!-- 结果配置信息 -->
            
                <result name="success" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                
                <!-- 应用拦截器 -->
                <interceptor-ref name="sessionNull"/>
                <interceptor-ref name="defaultStack"/>
               
            </action>
            
            <action name="adminlogin" class="cn.nit.action.AdminAction">
             <!-- 结果配置信息 -->
                <result name="success">/admin/index.jsp</result>
                <result name="error">/admin/adminLogin.jsp</result>
                
                <!-- 应用拦截器 -->
                
            </action>
        </package>
    </struts>
      

  4.   


    message No result defined for action cn.nit.action.TeacherAction and result logindescription The requested resource (No result defined for action cn.nit.action.TeacherAction and result login) is not 
    这个和拦截器错误有啥关系
    在这个上public String execute(){
    加上@Override
      

  5.   

    另,继承extends AbstractInterceptor这个比较好
      

  6.   

    return Action.LOGIN //配置文件中没有login的没配置吧
      
      

  7.   

    每次重启服务器的时候,session被清了,所以会执行 
    if (req.getSession().getAttribute("username") == null) {
                return Action.LOGIN;//这个还没处理,只是暂时写个
    在配置文件里找不到login的配置信息,所以出错
      

  8.   

    要加上全局
    <global-results>
    <result name=”login”>/login.jsp</result>
    </global-results>
    或把它弄成默认拦截器
      

  9.   


            <action name="teacherlogin" class="cn.nit.action.TeacherAction">
            
                <!-- 结果配置信息 -->
                
                <result name="success" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                
                <!-- 应用拦截器 -->
                <interceptor-ref name="sessionNull"/>
                <interceptor-ref name="defaultStack"/>
               
            </action>
    改为:
            <action name="teacherlogin" class="cn.nit.action.TeacherAction">
            
                <!-- 结果配置信息 -->
                
                <result name="success" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                
                <!-- 应用拦截器 -->
                <interceptor-ref name="sessionNull"/>
               
            </action>
    试试不行再说
      

  10.   

    意思就是默认的拦截器Struts会自动的调用 不用显示的声明
      

  11.   

    今天我又试了试,结果真奇怪,无论弄没弄<global-results>
           <result name="login">/login.jsp</result>
    </global-results>
    都能成功登录,奇怪,怎么会这样,我没有把<interceptor-ref name="defaultStack"/>删掉;这是什么回事?
      

  12.   

    <interceptor-ref name="sessionNull"/>
     <interceptor-ref name="defaultStack"/>
    会找下边拦截器的
      

  13.   

    我不加拦截器,如果直接使用http://localhost:8080/.../login.action,这个会自动拦截,但是我在struts.xml文件中<result>写成了这样(加了type="redirect")<result name="success" type="redirect">/teacher/index.jsp</result>
    但是如果用户直接输入:http://localhost:8080/..../teacher/index.jsp,则可以直接进入到里面,就没有拦截到?可以直接登录到那个页面,不知道如何拦截?我写的那个拦截器好像没用。
      

  14.   

    欢迎各位java大侠、小虾加入QQ高级群:45271133  群满500人为止,热烈邀请大侠们加入指点~!
      

  15.   

    不用加的 
    在strut.xml中  <package name="login" extends="struts-default">
    你已经继承了struts-default 就会默认的把这个加载到里面去 不用显示的给出
    你试了 删除<interceptor-ref name="defaultStack"/>了吗?
    试试 不行再说
      

  16.   


    试了,还是不行,还是下面的错误HTTP Status 404 - No result defined for action cn.nit.action.LoginAction and result login--------------------------------------------------------------------------------type Status reportmessage No result defined for action cn.nit.action.LoginAction and result logindescription The requested resource (No result defined for action cn.nit.action.LoginAction and result login) is not available.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.14
    还是这个错误,要不我加下你QQ,帮我弄一下
    QQ:286243808做这个东西我有碰多问题哦。同一用户不能同时登录(http://topic.csdn.net/u/20091012/12/b340ba22-58f9-4351-aba0-1986f447dca5.html?seed=1652258264&r=60361941#r_60361941)估计后面在实现的时候还有很多问题。
      

  17.   

    我把代码改了:
    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>
        <package name="login" extends="struts-default">
         <!-- 配置拦截器 -->
         <interceptors>
         <interceptor name="sessionNull" class="cn.nit.interceptor.SessionNullInterceptor"/>
         </interceptors>
        
         <global-results>
         <result name="login">/error/error.jsp</result>
         </global-results>
        
        
            <action name="login" class="cn.nit.action.LoginAction">
            
             <!-- 结果配置信息 -->
            
             <result name="admin" type="redirect">/admin/index.jsp</result>
                <result name="teacher" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                <result name="hadLogined">/error/hadLogined.jsp</result>
                <!-- 应用拦截器 -->
                
                <interceptor-ref name="sessionNull"/>
                
            </action>
        </package>
    </struts>拦截器:package cn.nit.interceptor;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;/**
     * session为空的拦截器
     * @author Administrator
     *
     */
    //拦截器实现Interceptor接口
    public class SessionNullInterceptor implements Interceptor { public void destroy() {
    // TODO Auto-generated method stub

    } public void init() {
    // TODO Auto-generated method stub

    } public String intercept(ActionInvocation invocation) throws Exception {
    // TODO Auto-generated method stub
    HttpServletRequest req = ServletActionContext.getRequest();
    if (req.getSession().getAttribute("username") == null) {
    System.out.println("用户未登录,不能直接登录....");
    return Action.LOGIN;
    } else {
    return invocation.invoke();
    }
    }
    }看这个拦截器,感觉有点问题,因为拦截是会执行“前置拦截”和“后置拦截”,那么在我点击“登录”之后,会执行这个拦截器代码,那么一点会执行到if (req.getSession().getAttribute("username") == null) {
            System.out.println("用户未登录,不能直接登录....");
            return Action.LOGIN;
    } else {
    return invocation.invoke();
    }
    }
    因为用户未登录,所以req.getSession().getAttribute("username")==null这部分肯定会执行,那么用户就肯定登录不进去了。是不是我的理解有误,可是我通过验证确实是这样的,用户名,密码,验证是一定正确的。看看图片:
    无论我输入什么内容,都会被拦截了,我相当无语。那拦截器应该怎么写?能写个出来我看看不。加分!!!
      

  18.   

    package com.mall.intercept;
    import java.util.Map;import org.apache.log4j.Logger;import com.mall.action.login.LoginAction;
    import com.mall.vo.Login;
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    /**
     * @author tom_hui
     * 
     */
    @SuppressWarnings("serial")
    public class LoginCheckerInterceptor extends AbstractInterceptor
    {
        private String userSessionKey = "userinfo";
        
        private String isCheckLogin   = "true";
        
        private Logger logger  = Logger.getLogger(LoginCheckerInterceptor.class);
        
        @SuppressWarnings("unchecked")
        public String intercept(ActionInvocation actionInvocation) throws Exception
        {
            Object action = actionInvocation.getAction();
            
            /** 如果设置拦截器不检查登陆 */
            if ("false".equalsIgnoreCase(isCheckLogin))
            {
                actionInvocation.invoke();
            }
            /** 如果是登陆Action,放其通行 */
            if (action instanceof LoginAction)
            {
                this.logger.info("登陆Action:" + LoginAction.class.getName());
                return actionInvocation.invoke();
            }
            
            /** 从session中得到UserInfo的信息 */
            Map session = actionInvocation.getInvocationContext().getSession();
            Login userInfo = (Login) session.get(userSessionKey);
            
            /** 如果Session中存在UserInfo对象 */
            if (userInfo != null)
            {
                this.logger.info("用户" + userInfo.getUsername() + "("
                        + userInfo.getUserid() + ")登陆了.");
                return actionInvocation.invoke();
                
            }
            /** 如果没有登陆 */
            else
            {
                return Action.LOGIN;
            }
        }
        
        public String getUserSessionKey()
        {
            return userSessionKey;
        }
        
        public void setUserSessionKey(String userSessionKey)
        {
            this.userSessionKey = userSessionKey;
        }
        
        public String getIsCheckLogin()
        {
            return isCheckLogin;
        }
        
        public void setIsCheckLogin(String isCheckLogin)
        {
            this.isCheckLogin = isCheckLogin;
        }
        
    }
      

  19.   

    你说的第2个问题楼上的代码是正确的
    也就是当你的action是登陆的action的时候,直接不做任何处理,继续执行你的登陆action
    你现在要肯定的是 你的action是否被你自己定义的拦截器拦截
    设置一个断点到拦截器代码里面看看是否执行到了
    xml里面的代码改为:<struts>
        <package name="login" extends="struts-default">
            <!-- 配置拦截器 -->
            <interceptors>
                <interceptor name="sessionNull" class="cn.nit.interceptor.SessionNullInterceptor"/>
            </interceptors>
            
            <global-results>
                <result name="login">/error/error.jsp</result>
            </global-results>
        添加:
            <default-interceptor-ref name="defaultStack"/>
        
            <action name="login" class="cn.nit.action.LoginAction">
            
                <!-- 结果配置信息 -->
                
                <result name="admin" type="redirect">/admin/index.jsp</result>
                <result name="teacher" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                <result name="hadLogined">/error/hadLogined.jsp</result>
                <!-- 应用拦截器 -->
                
                <interceptor-ref name="sessionNull"/>
                
            </action>
        </package>
    </struts>
      

  20.   

    <?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>
        <package name="login" extends="struts-default">
            <!-- 配置拦截器 -->
            <interceptors>
                <interceptor name="sessionNull" class="cn.nit.interceptor.SessionNullInterceptor"/>
            </interceptors>
            
            <global-results>
                <result name="login">/error/error.jsp</result>
            </global-results>
            
            
            <action name="login" class="cn.nit.action.LoginAction">
                <!-- 应用拦截器 -->
                
                <interceptor-ref name="sessionNull"/>

                <!-- 结果配置信息 -->
                
                <result name="admin" type="redirect">/admin/index.jsp</result>
                <result name="teacher" type="redirect">/teacher/index.jsp</result>
                <result name="error" type="redirect">/index.jsp</result>
                <result name="hadLogined">/error/hadLogined.jsp</result>
               
                
            </action>
        </package>
    </struts>
    你这样试试,把拦截器放在返回值的前面。