structs2.0配置
首先 我建立了struct项目
在src下建立了个包叫:org.action,建立一个类叫:StrutsAction
StrutsAction类内容如下:
package org.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class StrutsAction extends ActionSupport{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String execute()throws Exception
{
if(getName().equals("hello"))
{
return "success";
}
else
return "error";
}
}
index.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'index.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="struts.action" method="post">
请输入姓名:<input type="text" name="name"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
error.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'error.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>
错误 <br>
</body>
</html>
welcome.jsp如下 :
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'welcome.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>
hello!
</body>
</html>
我已经导入struts相应的jar包用bulid path方式
structs.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="org.action" extends="struts-default">
<action name="struts.action" class="org.action.StrutsAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
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">
<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>
打入地址:http://localhost:8080/struts2/index.jsp
输入hello
提交后,页面显示如下:
HTTP Status 404 - /struts2/struts.action
type Status report
message /struts2/struts.action
description The requested resource (/struts2/struts.action) is not available.
Apache Tomcat/7.0.2
不知什么原因,求赐教
[解决办法]
<action name="struts.action" class="org.action.StrutsAction">
<result name="success">/welcome.jsp</result>
HTTP Status 404 - /struts2/struts.action
取名不规范,正常的action的name不要带上.action,因为这是struts2默认的。比如name="xxx",
url:/工程名/xxx 或者/工程名/xxx.action。
如果一定要这样写,那么就还得在你的<form action="struts.action.action" method="post">
试试吧!
[解决办法]
lz,你的程序我运行过了,就两个地方,改了你就可以运行了。
1.struct.xml文件名写错了,应该是strut.xml
2.action的name不要带写后缀,对应index.jsp里面
3.我改过的strut.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="org.action" extends="struts-default">
<action name="struts" class="org.action.StrutsAction">
<result name="success">/WEB-INF/jsp/welcome.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
</package>
</struts>