我用struts2做了一个登录的小例子,很简单的,一个输入页面(index.jsp),一个action(类名:LogonAction),还有两个页面,一个成功,一个失败页面,这两个页面都很简单。情况是这样的,当我把struts.xml里的action名字改为logon时总是报
No result defined for action com.yan.web.action.LogonAction and result success,当我把随便改一下logon名字时比如改为logon1就可以正常运行,在网上找struts2的保留字段也找到什么,奇怪,请教各位。我把我用到的代码贴在下边:struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><!-- START SNIPPET: xworkSample -->
<struts> <package name="default" extends="struts-default">
<action name="logon" class="com.yan.web.action.LogonAction">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
</struts><!-- END SNIPPET: xworkSample -->index.jsp<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<% String base = request.getContextPath(); %></head><body>
<form action="logon.action" method="post">
<div style="color:red"><s:fielderror /></div>
<!-- 读取显示提示信息 -->
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="user.userName"/>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="user.password"/>
</td>
</tr>
<tr>
<td>
年龄:
</td>
<td>
<input type="text" name="user.userLevel"/>
</td>
</tr>
<tr>
<td>
生日:
</td>
<td>
<input type="text" name="user.agentNumber"/>
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</table>
</form>
</body>
</html>LogonAction.javapackage com.yan.web.action;import com.opensymphony.xwork2.ActionSupport;
import com.yan.entity.User;public class LogonAction extends ActionSupport {
private static final long serialVersionUID = 4991026832999229787L;
private User user;
@Override
public String execute() {
return "success";
} /**
* @return the user
*/
public User getUser() {
return user;
} /**
* @param user
* the user to set
*/
public void setUser(User user) {
this.user = user;
}
}user.javapackage com.yan.entity;/**
* User entity. @author MyEclipse Persistence Tools
*/public class User implements java.io.Serializable { // Fields private Integer id;
private String userName;
private String password;
private String userLevel;
private String agentNumber; // Constructors /** default constructor */
public User() {
} /** full constructor */
public User(String userName, String password, String userLevel,
String agentNumber) {
this.userName = userName;
this.password = password;
this.userLevel = userLevel;
this.agentNumber = agentNumber;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public String getUserLevel() {
return this.userLevel;
} public void setUserLevel(String userLevel) {
this.userLevel = userLevel;
} public String getAgentNumber() {
return this.agentNumber;
} public void setAgentNumber(String agentNumber) {
this.agentNumber = agentNumber;
}}
代码就上面这些
No result defined for action com.yan.web.action.LogonAction and result success,当我把随便改一下logon名字时比如改为logon1就可以正常运行,在网上找struts2的保留字段也找到什么,奇怪,请教各位。我把我用到的代码贴在下边:struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><!-- START SNIPPET: xworkSample -->
<struts> <package name="default" extends="struts-default">
<action name="logon" class="com.yan.web.action.LogonAction">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
</struts><!-- END SNIPPET: xworkSample -->index.jsp<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<% String base = request.getContextPath(); %></head><body>
<form action="logon.action" method="post">
<div style="color:red"><s:fielderror /></div>
<!-- 读取显示提示信息 -->
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="user.userName"/>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="user.password"/>
</td>
</tr>
<tr>
<td>
年龄:
</td>
<td>
<input type="text" name="user.userLevel"/>
</td>
</tr>
<tr>
<td>
生日:
</td>
<td>
<input type="text" name="user.agentNumber"/>
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</table>
</form>
</body>
</html>LogonAction.javapackage com.yan.web.action;import com.opensymphony.xwork2.ActionSupport;
import com.yan.entity.User;public class LogonAction extends ActionSupport {
private static final long serialVersionUID = 4991026832999229787L;
private User user;
@Override
public String execute() {
return "success";
} /**
* @return the user
*/
public User getUser() {
return user;
} /**
* @param user
* the user to set
*/
public void setUser(User user) {
this.user = user;
}
}user.javapackage com.yan.entity;/**
* User entity. @author MyEclipse Persistence Tools
*/public class User implements java.io.Serializable { // Fields private Integer id;
private String userName;
private String password;
private String userLevel;
private String agentNumber; // Constructors /** default constructor */
public User() {
} /** full constructor */
public User(String userName, String password, String userLevel,
String agentNumber) {
this.userName = userName;
this.password = password;
this.userLevel = userLevel;
this.agentNumber = agentNumber;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public String getUserLevel() {
return this.userLevel;
} public void setUserLevel(String userLevel) {
this.userLevel = userLevel;
} public String getAgentNumber() {
return this.agentNumber;
} public void setAgentNumber(String agentNumber) {
this.agentNumber = agentNumber;
}}
代码就上面这些
我研究了一下,问题可能在struts.xml里面
<package name="default" extends="struts-default">
<action name="logon" class="com.yan.web.action.LogonAction">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>我试了不光是logon如果 name和action的名字相同的话都是不行的,都报一样的错,比如:
<package name="default" extends="struts-default">
<action name="logon1" class="com.yan.web.action.Logon1Action">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
如果这样改,用了namespace后就不会报错了,如:
<package name="sys" extends="struts-default" namespace="/sys">
<action name="logon" class="com.yan.web.action.LogonAction" method="toDo">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
当然在index.jsp里面要改action的提交路径:<form action="sys/yan.action" method="post">
现在正在网上搜索struts2的名字空间,看看是不是能发现点问题
不知为什么,你现在解决了吗? 能给说说是怎么回事吗 !