struts2.2.3配置问题,请朋友们帮忙

chuxue1342 2011-05-14 05:57:17
先讲一下我的工发环境:
jdk1.6.0_24+apache-tomcat-7.0.12+Eclipse目前最新版

我下载了最新的:struts2.2.3

jar文件放在:WebContent\WEB-INF\lib,常用的都包括了
struts.xml放在:WebContent\WEB-INF\classes下,配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
* $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->

<!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.action.extension" value="action,do" />
<!-- 声明包 -->
<package name="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="userAction" class="com.action.UserAction">
<!-- 添加成功的映射页面 -->
<result name="add">user_add.jsp</result>
<!-- 更新成功的映射页面 -->
<result name="update">user_update.jsp</result>
</action>
</package>

<!-- 声明包 -->
<package name="myPackage2" extends="struts-default">
<!-- 定义action -->
<action name="TestAction" class="com.action.TestAction">
<!-- 处理成功的映射页面 -->
<result>success.jsp</result>
</action>
</package>
</struts>


web.xml放在:\WebContent\WEB-INF下,配置如下:

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


index.jsp文件:
<a href="TestAction.action">struts2</a>
<a href="userAction!add">添加用户</a>
<br>
<a href="userAction!update">更新用户</a>

TestAction文件:


package com.action;

import com.opensymphony.xwork2.ActionSupport;
/**
* Map类型的request、session、application
*/
public class TestAction extends ActionSupport {

private static final long serialVersionUID = 1L;

public TestAction(){
}

/**
* 请求处理方法
* @return String
*/
public String execute() throws Exception{

return SUCCESS;
}
}


问题描述如下:
按照目前的配置,index.jsp中三个链接都访问不了,错误类似:
HTTP Status 404 - /myNewProject/userAction!add
The requested resource (/myNewProject/userAction!add) is not available.


如果我把struts.xml文件中这段配置:
<constant name="struts.action.extension" value="action,do" />
注释掉的话

添加,更新用户可以访问,但:struts2这个链接还是用不了,错误如下一样:
The requested resource (/myNewProject/TestAction.action) is not available.


所有jsp文件都在一个目录下,请各位大侠们指教!
...全文
1848 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magiccrystal 2011-06-10
  • 打赏
  • 举报
回复
加了也没用
chenqiangjsj 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chuxue1342 的回复:]

杯具了,链接改为如下格式:
HTML code

<a href="TestAction!execute.action">struts2</a>
<a href="userAction!add.do">添加用户</a>
<br>
<a href="userAction!update.do">更新用户</a>



全部可以,求解释!怎样才能TestAction.action这样访……
[/Quote]
我也遇到了这个问题,终于知道原因了,在struts2-core-2.2.3.jar文件中的org.apache.struts2包下有一个default.properties里面有这么一行定义:
struts.action.extension=action,,
所以后面加上.action就可以。
chuxue1342 2011-05-17
  • 打赏
  • 举报
回复
至今还未解决,难道要帖了?
问题我再描述一下:
<a href="TestAction.action">struts2</a>报错
<a href="TestAction!execute.action">struts2</a>可以

execute方法不是默认执行的吗?为什么我不传过去执行不了?

w_huining 2011-05-16
  • 打赏
  • 举报
回复
照着struts2包里的例子看看。
chuxue1342 2011-05-16
  • 打赏
  • 举报
回复
谢谢楼上几位朋友:
关于redlotus_lyn回答,我类里面有execute方法,但没有执行,我现在就是因为这个问题实现不了!
lovemekc 2011-05-16
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 icecreamfuture 的回复:]
因为<result>写错了,没有名,name="success"没写,肯定找不到页面。
[/Quote]
冒似如果是返回success的话是可以省略的。。
fhorseface 2011-05-16
  • 打赏
  • 举报
回复
是这样的,配置 <constant name="struts.action.extension" value="action,do" />这个地方写成 <constant name="struts.action.extension" value="action,do,," />这样试试
redlotus_lyn 2011-05-16
  • 打赏
  • 举报
回复
index.jsp文件:
<a href="TestAction.action">struts2</a>
<a href="userAction!add">添加用户</a>
<a href="userAction!update">更新用户</a>

以上三个连接都运行不了:

1.

<a href="userAction!add">添加用户</a>

这属于struts2的动态方法调用,使用时必须在最后加上请求扩展名。

例:
<a href="userAction!add.action">添加用户</a>

2.

<a href="TestAction.action">struts2</a>

这是正常使用方式。

<action name="TestAction" class="com.action.TestAction">
<result>success.jsp</result>
</action>

没有指定method属性,框架默认执行execute()方法,所以必须在TestAction中加入execute()方法。
icecreamfuture 2011-05-16
  • 打赏
  • 举报
回复
因为<result>写错了,没有名,name="success"没写,肯定找不到页面。
chuxue1342 2011-05-15
  • 打赏
  • 举报
回复
谢谢lgh1117的回复~~但没有解决
<a href="TestAction.action">struts2</a>
不能访问的问题!
我现在是怀疑,如果不指定方法的话,默认是不是不会执行excute这个方法!
chuxue1342 2011-05-15
  • 打赏
  • 举报
回复
谢谢Inhibitory的回复!
但我现在的问题是:
<a href="TestAction.action">struts2</a>
调试时在初始化的时候就失败了,并未执行excute方法,直接用:
<a href="TestAction!excute.action">struts2</a>
却是可以的!我现在就想不带默认的方法访问,该如何解决!
chenyinglong0712 2011-05-15
  • 打赏
  • 举报
回复
楼主结贴啊
Inhibitory 2011-05-15
  • 打赏
  • 举报
回复
这是我的Struts的配置,为同一个类中的每个方法都定义一个url,如runmlm,使用时是<a href="runmlm.action">gooooo</a>,因为我使用的是ajax,所以里面没有配置result元素。
  <action name="addmlm" class="hb.action.MlmAction" method="addMlm" /> 
<action name="runmlm" class="hb.action.MlmAction" method="runMlm" />
<action name="updatemlm" class="hb.action.MlmAction" method="updateMlm" />
<action name="compilemlm" class="hb.action.MlmAction" method="compileMlm" />
<action name="requestast" class="hb.action.MlmAction" method="requestAST" />
<action name="requestmlm" class="hb.action.MlmAction" method="requestMlm" />
<action name="requestmlms" class="hb.action.MlmAction" method="requestMlms" />
Inhibitory 2011-05-15
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 chuxue1342 的回复:]

谢谢lgh1117的回复~~但没有解决
<a href="TestAction.action">struts2</a>
不能访问的问题!
我现在是怀疑,如果不指定方法的话,默认是不是不会执行excute这个方法!
[/Quote]
对的,默认就是执行execute方法的
lgh1117 2011-05-14
  • 打赏
  • 举报
回复
换成如下方式,你会更加的清楚

<package name="myPackage" extends="struts-default" namespace="/">
<!-- 定义action -->
<action name="userAction_*" method="{1}" class="com.action.UserAction">
<!-- 添加成功的映射页面 这个路径要用绝对的,绝对与应用webroot的路径,相对的是相对于当前的servlet路径-->
<result name="add">/user_add.jsp</result>
<!-- 更新成功的映射页面 -->
<result name="update">/user_update.jsp</result>
</action>
</package>


访问的连接是userAction_index.action,index是你对应在action中的名字
chuxue1342 2011-05-14
  • 打赏
  • 举报
回复
我就是怕我们加载到,所以从官网Example复制过来的,全部如下:
commons-logging-1.1.1.jar
javassist-3.11.0.GA.jar
commons-collections-3.1.jar
asm-3.1.jar
asm-commons-3.1.jar
asm-tree-3.1.jar
commons-beanutils-1.7.0.jar
freemarker-2.3.16.jar
xpp3_min-1.1.3.4.O.jar
ezmorph-1.0.3.jar
json-lib-2.1-jdk15.jar
xstream-1.2.2.jar
commons-lang-2.5.jar
ognl-3.0.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
xwork-core-2.2.3.jar
struts2-core-2.2.3.jar
struts2-rest-plugin-2.2.3.jar
struts2-convention-plugin-2.2.3.jar

Isnotsuitable 2011-05-14
  • 打赏
  • 举报
回复
你加了哪几个jar包
chuxue1342 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 chenyinglong0712 的回复:]
我帮你试了一下,你这个问题,使用userAction!add.action.可以搞定,但是不加.action报404的错误。找不到路径,问题就是你重新配置了<constant name="struts.action.extension" value="action,do" />这个xwork中的配置。所以,你的所有请求都必须添加后缀,才能通过请求...............这样的话,会把你在we……
[/Quote]

这两个链接我知道,但前面那个struts呢?
TestAction.action
chenyinglong0712 2011-05-14
  • 打赏
  • 举报
回复
我帮你试了一下,你这个问题,使用userAction!add.action.可以搞定,但是不加.action报404的错误。找不到路径,问题就是你重新配置了<constant name="struts.action.extension" value="action,do" />这个xwork中的配置。所以,你的所有请求都必须添加后缀,才能通过请求...............这样的话,会把你在web.xml中的配置给替换掉。不是“/*”而是“/action”或“/do”
chenyinglong0712 2011-05-14
  • 打赏
  • 举报
回复
你这个问题就处在,你的后缀名没有写:你把
<a href="userAction!add">添加用户</a>
<a href="userAction!update">更新用户</a>

这两个加上后缀:userAction!add.action,就可以搞定了。。。。。
加载更多回复(11)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧