环境:eclipse 3.5,tomcat 5.5,struts2.0.11.2代码如下: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>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="struts2" namespace="/" extends="struts-default">
<action name="login" class="com.test.LoginAction" method="execute">
<result name="success">index.jsp</result>
</action></package></struts>LoginAction.javapublic class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
return "success";
}
Login.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
${username}
${password}
</body>
</html>问题:
HTTP Status 404 - /struts2/logintype Status reportmessage /struts2/logindescription The requested resource (/struts2/login) is not available.
直接
http://localhost:8080/struts2/login.action,可以但是表达提交方式无法访问到action
不知道什么原因,望解决!谢谢!
<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>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="struts2" namespace="/" extends="struts-default">
<action name="login" class="com.test.LoginAction" method="execute">
<result name="success">index.jsp</result>
</action></package></struts>LoginAction.javapublic class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
return "success";
}
Login.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
${username}
${password}
</body>
</html>问题:
HTTP Status 404 - /struts2/logintype Status reportmessage /struts2/logindescription The requested resource (/struts2/login) is not available.
直接
http://localhost:8080/struts2/login.action,可以但是表达提交方式无法访问到action
不知道什么原因,望解决!谢谢!
改成
<action name="login.action" class="com.test.LoginAction" method="execute">试试
http://localhost:8080/struts2/login.action,可以但是表达提交方式无法访问到action这是因为你form上的路径写的有问题,所以报:The requested resource (/struts2/login) is not available.的问题,知道了问题,就修改你的form的action路径:工程名/login.jsp文件的相对路径/login.action
<s:form action="login" method="post">
改为:
<s:from action="login.action" method="post">