Struts2 无法访问资源
学习Struts2,出现
HTTP Status 404 - /webDemo/HelloWorld.action
--------------------------------------------------------------------------------
type Status report
message /webDemo/HelloWorld.action
description The requested resource (/webDemo/HelloWorld.action) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.26
配置如下
Apache Tomcat/7.0.26
Struts struts-2.3.1.2
----------------------------------------------------------
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>
<!-- Configuration for the default package. -->
<package name="hello" extends="struts-default">
<action name="HelloWorld" class="com.hello.action.HelloWorldAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
----------------------------------------------------------------------------------------
web.xml
<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>/*</url-pattern>
</filter-mapping>
----------------------------------------------------------------------------------------------
HelloWorld.java
package com.hello.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import org.omg.PortableInterceptor.SUCCESSFUL;
public class HelloWorldAction implements Action {
private String message;
public String execute() throws Exception {
this.message ="Hello world";
ActionContext context=ActionContext.getContext();
Map session=context.getSession();
session.put("message", message);
return SUCCESS;
}
}
-----------------------------------------------------------------------------------------------
----------------------------------------------------------
输入: http://localhost:8080/webDemo/HelloWorld.action
就出现在上面的错误的了,这个是按书的例子,主要是环境不同,安装的struts版本不同,但我想问题不大吧,还请高手指出配置哪有问题了
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
参考了其它帖子,把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>
<!-- Configuration for the default package. -->
<package name="hello" extends="struts-default" namespace="/">
<action name="HelloWorld" class="com.hello.action.HelloWorldAction" method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
但还是没用
尝试一下,在页面写个链接<a href="/HelloWord">HelloWord</a>,点击一下,看一下结果如何?
刚才看了下Console, tomcat在启动时报了如下错误
2012-4-6 9:17:46 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:201)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:180)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:436)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4638)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
... 22 more
看上去是struts配置不对,造成Class没找到,还得继续寻找
终于搞好了,参考了struts2官网的例子
http://struts.apache.org/2.3.1.2/docs/create-struts-2-web-application-with-artifacts-in-web-inf-lib-and-use-ant-to-build-the-application.html
里面lib的引用如下
Find and copy to WEB-INFlib these files (note X.X.X.X.jar refers to the version number):
程序没有问题,我把配置整理如下,给大家参考下
环境配置
Apache Tomcat/7.0.26
Struts struts-2.3.1.2
------------------------------
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>
<!-- Configuration for the default package. -->
<package name="hello" extends="struts-default" >
<action name="HelloWorld" class="com.hello.action.HelloWorldAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
----------------------------------------------------
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to J2EE</display-name>
<description>
Welcome to J2EE
</description>
<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>
------------------------------------------------------
java
package com.hello.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.omg.PortableInterceptor.SUCCESSFUL;
public class HelloWorldAction extends ActionSupport {
private String message;
public String execute() throws Exception {
this.message ="Hello world";
ActionContext context=ActionContext.getContext();
Map session=context.getSession();
session.put("message", message);
return SUCCESS;
}
}
------------------------------------------------
结构如图
运行http://localhost:8080/webDemo/HelloWorld.action
出现正确结果
谢谢大家
总结一句,java的学习要不就严格按书本上所说,运行的配置环境保持一致
要不就要到官网上先看下,新发布的包有什么要求,和旧的不同了,否则遇到这些问题让新手实在是很难受
恩,尽信书则不如无书,问题解决了即使总结这种精神很好,值得鼓励!
你的struts2是什么版本,楼上的流云,万里晴,struts2不配置namespace可以运行,默认namespace=""
这不是问题的关键。
2.3.1.2
加上namespace="/" 也没有用
你改成这样试试 :
<package name="hello" namespace="/" extends="struts-default">
你没有配置namespace,至少也应该把它设为namespace=""或者namespace="/"
不是吧,它没写namespace阿
我错了,是我想当然了,不好意思,呵呵
是访问地址的问题吧,应该是不能直接写.action吧,
Struts.xml
<package name="hello" extends="struts-default">
所以,你要访问的地址应该是:
http://localhost:8080/webDemo/hello/HelloWorld.action