日期:2014-05-20  浏览次数:20709 次

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);