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
不知什么原因,求赐教
[最优解释]
可以的话,把程序发我qq28205968,我帮你看看。
[其他解释]
<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如下:
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 访问路径的后缀 -->
<constant name="struts.action.extension" value="action,do" />
<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>
[其他解释]
改了报什么错啊?配置就那么回事!翻来覆去网上都说得明明白白的!
[其他解释]
顺便去查一下webapp下面的工程名是不是你建的工程名
[其他解释]
at org.apache.catalina.startup.Catalina.load(Catalina.java:624)
at org.apache.catalina.startup.Catalina.load(Catalina.java:649)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
at org.apache.catalina.connector.Connector.initInternal(Connector.java:983)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
... 12 more
Caused by: java.net.BindException: Address already in use: JVM_Bind <null>:8009
at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:406)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:610)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:429)
at org.apache.catalina.connector.Connector.initInternal(Connector.java:981)
... 13 more
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:141)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:49)
at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:393)
... 16 more
第一个我百度过,是端口占用。但是我输入http://localhost:8080/struts2/index.jsp
还是可以访问index.jsp
[其他解释]
端口被占用 ,查看哪个服务占用了删除掉 ,或者重启电脑
[其他解释]
可能有两个原因:1.action名字不要带.action。
2.加上namespace="/"。
[其他解释]