想用Struts2 做一个简单的登录演示结果老是报错误:
严重: Could not find action or result
There is no Action mapped for action name loginAction. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)希望各位大虾帮忙指点一下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.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>LoginAction.javapackage com.topking.bbs.action.client;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { /**
*
*/
private String userName;
private String passWord;
private static final String SUCESS = "login_sucess";
private static final String FAIL = "login_fail";
private static final long serialVersionUID = 4809991810934522266L;
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{
if(this.getUserName().equals("jacky")&&this.getPassWord().equals("123")){
return SUCESS;
}
return FAIL;
}
public static void main(String[] args) throws Exception{
LoginAction action = new LoginAction();
action.setUserName("jacky");
action.setPassWord("123");
System.err.println(action.execute());
}
}
struts.xml配置如下:(struts.xml放在WEB-INF下)<?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="exmp" extends="struts-default" namespace="/mystruts">
<action name="loginAction" class="com.topking.bbs.action.client.LoginAction">
<result name="login_sucess">/index.jsp</result>
<result name="login_fail">/_client/regist.jsp</result>
</action>
</package>
</struts>前台调action的页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s2" uri="/struts-tags" %>
<%
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="css/default.css">
</head>
<body><center>
<h1>TOP-king</h1><hr/>
<a href="_client/regist.jsp">Regist</a>
<s2:form action="mystruts/loginAction.action" method="post">
userName:<input type="text" name="userName" /> passWord:<input type="password" name="passWord" /> <input type="submit" value="Login" />
</s2:form>
</center>
</body>
</html>
严重: Could not find action or result
There is no Action mapped for action name loginAction. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)希望各位大虾帮忙指点一下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.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>LoginAction.javapackage com.topking.bbs.action.client;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { /**
*
*/
private String userName;
private String passWord;
private static final String SUCESS = "login_sucess";
private static final String FAIL = "login_fail";
private static final long serialVersionUID = 4809991810934522266L;
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{
if(this.getUserName().equals("jacky")&&this.getPassWord().equals("123")){
return SUCESS;
}
return FAIL;
}
public static void main(String[] args) throws Exception{
LoginAction action = new LoginAction();
action.setUserName("jacky");
action.setPassWord("123");
System.err.println(action.execute());
}
}
struts.xml配置如下:(struts.xml放在WEB-INF下)<?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="exmp" extends="struts-default" namespace="/mystruts">
<action name="loginAction" class="com.topking.bbs.action.client.LoginAction">
<result name="login_sucess">/index.jsp</result>
<result name="login_fail">/_client/regist.jsp</result>
</action>
</package>
</struts>前台调action的页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s2" uri="/struts-tags" %>
<%
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="css/default.css">
</head>
<body><center>
<h1>TOP-king</h1><hr/>
<a href="_client/regist.jsp">Regist</a>
<s2:form action="mystruts/loginAction.action" method="post">
userName:<input type="text" name="userName" /> passWord:<input type="password" name="passWord" /> <input type="submit" value="Login" />
</s2:form>
</center>
</body>
</html>
楼主把命名空间去掉试试看
<package name="exmp" extends="struts-default" namespace="/mystruts">
去掉 namespace="/mystruts"然后JSP里改成
<s2:form action="loginAction" method="post">
试试.
将
<action name="loginAction" class="com.topking.bbs.action.client.LoginAction">
<result name="login_sucess">/index.jsp </result>
<result name="login_fail">/_client/regist.jsp </result>
</action>
更改为
<action name="loginAction" class="com.topking.bbs.action.client.LoginAction">
<result name="sucess">/index.jsp </result>
<result name="error">/_client/regist.jsp </result>
</action> 在LoginAction中将
public String execute() throws Exception{
if(this.getUserName().equals("jacky")&&this.getPassWord().equals("123")){
return SUCESS;
}
return FAIL;
}更改为
public String execute() throws Exception{
if(this.getUserName().equals("jacky")&&this.getPassWord().equals("123")){
return SUCESS;
}
return ERROR;
}你返回的SUCCESS,FAIL在struts.xml配置文件中找不到
你为什么要覆盖呢
感觉没有必要使用Struts2自定义的比较好
而且 sturts的标签库中form标签有属性为namespace应该和action中定义的action值一样
建议楼主这样写,维护起来比较方便,如果写成
<s2:form action="/工程名/mystruts/loginAction" method="post" namespace="/mystruts">
也可以,不过它会有警告
<s2:form action="./mystruts/loginAction.action" method="post">
没问题了。