首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

jquery+struts2 $.post()提交到指定action方法异常

2012-04-10 
jquery+struts2 $.post()提交到指定action方法错误我用jquery1.7+struts2.2.1做一个注册例子学习jquery AJ

jquery+struts2 $.post()提交到指定action方法错误
我用jquery1.7+struts2.2.1做一个注册例子学习jquery AJAX,代码如下

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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>

struts.xml主要代码:

<struts>

<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.test.action.RegisterAction">
<result>/register.jsp</result>
</action>
</package>

</struts>

register.jsp主要代码:
<script type="text/javascript" src="<%=request.getContextPath() %>/jquery/jquery-1.7.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/register.js"></script>
<form name="form1" method="post" action="">
  <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  <td colspan="3"><div align="center">用户注册</div></td>
  </tr>
  <tr>
  <td width="30%">用户名:</td>
  <td width="25%"><label>
  <input name="user.userName" type="text" id="userName">
  </label></td>
  <td width="45%"><span id="userNameMessage"></span>&nbsp;</td>
  </tr>
  </table>
</form>

RegisterAction.java :

import com.test.model.User;

public class RegisterAction extends ActionSupport {
/**

*/
private static final long serialVersionUID = -3045347470138967207L;
User user;
public String execute(){
//System.out.println(user.getUserName());
System.out.println("excute....");
return SUCCESS;
}
public String validateUser(){
System.out.println("validateUser....");
return SUCCESS;
}

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

}
register.js代码:

$(document).ready(function() {
$("#userName").blur(function(){
  if($("#userName").val()==""){
  $("#userNameMessage").html("请输入用户名!")
  $("#userName").focus();
  }else{
  $.post("register",{userName:$("#userName").val()},function(result){
  $("#userNameMessage").html(result);


  });
  }
 
  });
});

遇到的问题:
$.post("register",{userName:$("#userName").val()},function(result){
  $("#userNameMessage").html(result);
  });
URL只能这样写register访问到RegisterAction的execute方法,写成register!validateUser执行不到validateUser方法。在网上别人说写成:register!validateUser.action我试了也不行,报错找不到action 求高手指点什么原因,到底应该怎么写?


[解决办法]
struts.xml文件配错了。
<struts>

<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<action name="register_*" class="com.test.action.RegisterAction" method="{1}">
<result>/register.jsp</result>
</action>
</package>

</struts>

之后
$.post("register_validateUser",{............
[解决办法]

XML code
struts.xml主要代码:<struts><constant name="struts.enable.DynamicMethodInvocation"value="false" /><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><action name="register" class="com.test.action.RegisterAction"><result>/register.jsp</result></action></package></struts> 

热点排行