问题描述
一个破拦截器搞了半天没发现哪里出问题了就是进不去自定义的拦截器里,下面贴代码: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.1.dtd"><struts><package name="root" namespace="/" extends="struts-default"> <!-- 拦截器 --><interceptors><!-- 自定义拦截器 --> <interceptor name="myoneInterceptor" class="com.niit.action.MyoneInterceptor"/> <!-- 自定义拦截器栈 --> <interceptor-stack name="appstack"> <interceptor-ref name="myoneInterceptor"/> <interceptor-ref name="defaultStack" /></interceptor-stack> </interceptors> <!--系统所用的拦截器栈名 --> <default-interceptor-ref name="appstack"/> <action name="MyAct" class="com.niit.action.Act"> <result name="input">MyJsp.jsp</result> </action></package></struts>java代码(在com.niit.action包里)Act类package com.niit.action;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class Act extends ActionSupport {public String login(){return "input";}}MyOneInterceptor类package com.niit.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class MyOneInterceptor extends AbstractInterceptor{public String intercept(ActionInvocation actionInvocation) throws Exception {// TODO Auto-generated method stubSystem.out.println("进入我的拦截器");Object action = actionInvocation.getAction();System.out.println("请求的对象名:" + action.getClass().getName());return actionInvocation.invoke(); //让url继续执行}}index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <s:form action="MyAct!login.action" method="get"> <input type="submit" value="提交"/> </s:form> </body></html>MyJsp.jsp<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ taglib uri="/struts-tags" prefix="s"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body>myjsp </body></html>web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
解决方案
1、<s:form action="MyAct!login.action" method="get"> 你开动态方法调用了吗?<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 2、有错误吗?3、如果不行clean下服务器试试
解决方案二:
大哥你这个问题是怎么解决的?
解决方案三:
把public class MyOneInterceptor extends AbstractInterceptor{ 改成public class MyOneInterceptor implements Interceptor{ 试试
解决方案四:
动态方法调用默认是开启的之所以访问不到action的方法,是因为你没有在struts.xml配置访问action的后缀<!-- 后缀 --><constant name="struts.action.extension" value="action" />
解决方案五:
这种demo犯不着纠结,直接百度个用了http://www.cnblogs.com/lihuiyy/archive/2012/03/29/2423569.html