因为有个项目需要用到Struts2, 所以照着书上的例子从最简单的HelloWorlod学起,可是总是报404找不到网页的错误,找了半天就是不知道什么原因,郁闷了 首先我的项目名称叫Struts2_HelloWorld,我在项目下的src目录下新建了一个HelloWorld类,如下:
package tutorial;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport
{
private static final long serialVersionUID = 1L;
private String name; public String getName
{
return name;
} public void setName(String name)
{
this.name = name;
} public String execute()
{
name = "Hello, " + name + "!"; return SUCCESS;
}
} 该目录下还有struts.properties文件和struts2.xml文件struts.properties文件内容如下:
struts.locale = en_GBstruts2.xml文件内容: <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>另外连个jsp文件分别是sayHello.jsp:
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="s" uri="/struts-tags"%> <body>
<h3:>Say "Hello" to:</h3>
<s:form action="HelloWorld">
Name:<s:textfield name="name"/>
<s:submit/>
</s:form>
</body>HelloWorld.jsp文件如下: <%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%> <body>
<h3><s:property value="name"/></h3>
</body>我现在浏览器输入http://localhost:8080/Struts2_HelloWorld/sayHello.jsp
我输入内容提交以后就报404的错误:
HTTP Status 404 - /Struts2_HelloWorld/HelloWorld--------------------------------------------------------------------------------type Status reportmessage /Struts2_HelloWorld/HelloWorlddescription The requested resource (/Struts2_HelloWorld/HelloWorld) is not available.
--------------------------------------------------------------------------------Apache Tomcat/5.5.20
后台出现两条警告:信息: Server startup in 7109 ms
2011-4-6 7:35:16 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: '/HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2011-4-6 7:35:18 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: '/HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value. 我在配置文件里面把路径弄了半天还是这个错,真是郁闷了
package tutorial;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport
{
private static final long serialVersionUID = 1L;
private String name; public String getName
{
return name;
} public void setName(String name)
{
this.name = name;
} public String execute()
{
name = "Hello, " + name + "!"; return SUCCESS;
}
} 该目录下还有struts.properties文件和struts2.xml文件struts.properties文件内容如下:
struts.locale = en_GBstruts2.xml文件内容: <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>另外连个jsp文件分别是sayHello.jsp:
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="s" uri="/struts-tags"%> <body>
<h3:>Say "Hello" to:</h3>
<s:form action="HelloWorld">
Name:<s:textfield name="name"/>
<s:submit/>
</s:form>
</body>HelloWorld.jsp文件如下: <%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%> <body>
<h3><s:property value="name"/></h3>
</body>我现在浏览器输入http://localhost:8080/Struts2_HelloWorld/sayHello.jsp
我输入内容提交以后就报404的错误:
HTTP Status 404 - /Struts2_HelloWorld/HelloWorld--------------------------------------------------------------------------------type Status reportmessage /Struts2_HelloWorld/HelloWorlddescription The requested resource (/Struts2_HelloWorld/HelloWorld) is not available.
--------------------------------------------------------------------------------Apache Tomcat/5.5.20
后台出现两条警告:信息: Server startup in 7109 ms
2011-4-6 7:35:16 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: '/HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2011-4-6 7:35:18 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: '/HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value. 我在配置文件里面把路径弄了半天还是这个错,真是郁闷了
Action类HelloWorld 代码如下:package tutorial;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name; public String getName {
return name;
}
public void setName(String name){
this.name = name;
} public String sayHelloWorld(){
name = "Hello, " + name + "!";
return "sayHello";
}
}
struts2的配置文件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="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="sayHello">/HelloWorld.jsp</result><!-- 注意前面的那个 “/” 表示根目录 -->
</action>
</package>
</struts>
<package name="admin_config" namespace="/main" extends="struts-default">引用时 /main/HelloWorld.action 就可以
Name:<s:textfield name="name"/>
<s:submit/>
</s:form>
LZ试试,把加粗的地方改一下