初学struts2·出现action找不到问题
废话少说·发代码
JSP页面:<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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 'myfilterJSP.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>
   <form action="user/myfilterAction" method="post">
   <input type="text" name="name"/><br>
   <input type="password" name="pwd"/>
   <input type="submit" value="走">
   </form>
  
  </body>
</html>配置文件:<?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.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK" /><!-- internationalization -->
<package name="user" extends="struts-default" namespace="/user">
<action name="userAdd"
class="com.bjsxt.struts2.user.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
<action name="user"
class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action>

<action name="myfilterAction"
class="com.cme.cen.action.MyFilterAction" method="getMessage">
<result name="myfilter">/success.jsp</result>
</action>
</package>
</struts>action类:package com.cme.cen.action;import com.opensymphony.xwork2.ActionSupport;public class MyFilterAction extends ActionSupport {
String name;
String paw;
public String getMessage() {
System.out.println(name);
System.out.println(paw);
return "myfilter";
}

public void setName(String name) {
this.name=name;
}

public void setPaw(String paw){
this.paw=paw;
}

public String getName() {
return name;
}

public String getpaw() {
return paw;
}
}为何访问不了action·出现了404错误?
顺便提一下,我原先是用默认的过滤器,结果是可以访问的,但是我自己编写一个过滤器,并且使用它的时候,就报错了,但是过滤器已经运行了,顺便贴出我的过滤器代码:package com.cme.cen.myfilter;import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;/*
 * 这个类是自己写的一个过滤器类,用于过滤乱中文码问题
 */
public class Myfilter implements Filter { public void destroy() {

}
//这个方法可以在servlet获取到参数之前先做好过滤·,对象arg2表示一个练·即执行完过滤之后将数据继续往下传递
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain china) throws IOException, ServletException {

request.setCharacterEncoding("GBK");
System.out.println("************************"+request.getParameter("name"));
china.doFilter(request, response);//继续往下传递参数
}
public void init(FilterConfig arg0) throws ServletException { }}
顺带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>chanise</filter-name>
<filter-class>com.cme.cen.myfilter.Myfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>chanise</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
代码比较多·只要能解决问题·我立马结贴·没并且全部分数奉上·拜托各位了!

解决方案 »

  1.   

    服务器没出错·就是浏览器报的错误·
    HTTP Status 404 - /Struts2_1000_CharacterEncoding/user/myfilterAction--------------------------------------------------------------------------------type Status reportmessage /Struts2_1000_CharacterEncoding/user/myfilterActiondescription The requested resource (/Struts2_1000_CharacterEncoding/user/myfilterAction) is not available.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.29
      

  2.   

    你的struts里面的namespace加了user这层路径了  jsp表单里面的action的值 不需要再加user这层路径!!
      

  3.   

     <form action="绝对路径/myfilterAction" method="post">  这样试下
      

  4.   

    把Action改成试下  action="<%=basePath%>user/myfilterAction"
      

  5.   

    回楼上,不加的话也错·而且·namespace里的路径是必须要的吧?
      

  6.   

    struts的核心就是拦截器! LZ干嘛要自己写呢!  
    具体的包 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      

  7.   

    不一定啊 namespace是你自己建的文件夹,或者其他的 你把jsp放里面,那么访问的时候就多了层路径,假如直接在webroot下面的不用加namespace!
      

  8.   

    引用:
    为何访问不了action·出现了404错误?
    顺便提一下,我原先是用默认的过滤器,结果是可以访问的,但是我自己编写一个过滤器,并且使用它的时候,就报错了,但是过滤器已经运行了,顺便贴出我的过滤器代码:你写的过滤器只是设置编码格式,想要实现分发还得加上struts的过滤器,由它来寻找action
      

  9.   

    但是,我的即使不使用namespace`也依然是错的哦··
      

  10.   

    当然啦 因为你访问的jsp确实在那个文件夹里面啊!!如果不在 namespace不用写!
      

  11.   

    说具体点好吗?我访问的JSP页面的确是直接在WEBROOT文件夹下的,但是表单提交的时候就报404··
      

  12.   

    你的意思说你的jsp就直接在webroot下面,不在webroot下面的子文件夹里面,那form表单里德action,和namespace不用加user这层路径!!!
      

  13.   

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

  14.   

    <form action="user/myfilterAction" method="post">是html标签,action="user/myfilterAction"要加.action
      

  15.   

    是不是应该配置上 struts2 的默认filter??<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
      

  16.   

     <form action="action的名字.action" method="post" namespace="/">
    如果你web配置文件没改的话应该是.action提交
      

  17.   

    .action还是出错··
    如果默认的过滤器的话,那重写的过滤器就需要到下一层才执行啦··但是两个的映射都一样··所以我想只用我的过滤器来顾虑掉乱码··
      

  18.   

    问题应该在这里吧,你自己写的提交路径是这个格式的<url-pattern>/*</url-pattern>
    而默认的提交路径应该是<url-pattern>*.action</url-pattern>
    所以你表单提交的时候应该这样写
    <form action="struts中action的名字" method="post" namespace="/">
    试试
      

  19.   


    不是必须的。 可以配置成 namespace=""
    http:localhost:8080/xxx/aaa/dosomething.action
    http:localhost:8080/xxx/aaa/bbb/dosomething.action
    http:localhost:8080/xxx/aaa/bbb/ccc/dosomething.action
    都可以匹配到这个package如果你配置成namespace="/user"的话,
    你要访问
    http:localhost:9090/xxx/user/dosomething.action
      

  20.   

    回楼上··你说的我都了解·但是我都试过了呢··真的还是404··我就怀疑是不是必须要使用struts2的过滤器了··
      

  21.   

    这个好像是必须得有,struts 的所有请求都要过它。看下这http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f732618187502884cd15c6735b36163bbca67175524289d27c1140e9091bacb13665377574ecc58edf0acabbe47c74d66163304add0050805ffe911165d620e60bb3f445b0fba72593d887809f040d9d05127af6a3d70c1714bc3eaa4771b0f7df1f554810ccba6c34a20f2c75ca3440c013a2be7439479ce298010c9525912751d0e974a73f62b504a56f0c5443b73db71f505664bb1161a8046b5fd3a90ea17b231763a149c3aeb0c5fc3989cbeb418f8acbb85fe577e7b5fd8802550157ed24cebccdb12a124f15afc8c961b425bd8afbca4bfe65d1&p=ce769a43c08205ec08e2927b4f4e&user=baidu&fm=sc&query=struts2%B5%C4FilterDispatcher%CA%C7%B2%BB%CA%C7%B1%D8%D0%EB%B5%C4&qid=f1c8206316d250fa&p1=9
      

  22.   


    你说的struts2的过滤器指的是什么? 难道是指mvc框架的核心拦截器啊?
    你指的是它?
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter我不知道你为什么不用这个而自己写一个,如果你用的是struts2.1.6版本或者以下版本,想自己解决编码bug的话。 你可以自己写一个Fitler并且配置在核心过滤器之前工作。懂我的意思了吗?
    也就是说public class Myfilter implements Filter {    public void destroy() {
            
        }    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain china) throws IOException, ServletException {
            
            request.setCharacterEncoding("GBK");
            System.out.println("************************"+request.getParameter("name"));
            china.doFilter(request, response);//继续往下传递参数
        }
        public void init(FilterConfig arg0) throws ServletException {    }} <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
    <filter-name>myf</filter-name>
    <filter-class>com.cme.cen.myfilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>myf</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>com.cme.cen.myfilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    </web-app>
    核心控制必须要的,没有它struts无法工作。
    你的辅助功能的过滤器,要放在核心过滤器之前!
      

  23.   

    如果你仅仅是想解决乱码问题,也不用搞这么麻烦。 把struts2换成2.1.8或者以上版本就好了。 这个是struts的一个bug,2.1.8以后修正了!