您好我正在尝试使用jquery ajax函数从我的jsp页面调用LoginAction.java。如果我没有使用ajax函数传递任何参数并且不在struts.xml的操作中添加param标签,那么我的调用正确发生但是当我在ajax调用中添加一个类型为object的param并在struts action中添加param标签时我的LoginAction类没有被调用或命中。 提前谢谢。
let email = $("email").val();
let fName = $("fName").val();
let lName = $("lName").val();
let password = $("pwd").val();
var newUser = new user(email,fName,lName,password);
alert(newUser);
$.ajax({
type: 'GET',
url:'register.action?user='+new?User,
success: function(data){
}});
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>
<constant name="struts.devMode" value="true" />
<package name="default" extends="json-default" namespace="/">
<action name="register" class="com.blogger.action.LoginAction" method="register">
<param name = "user">${user}</param>
<result name="SUCCESS" type="json"></result>
</action>
</package>
</struts>
LoginAction.java:
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register() {
System.out.println("in register function");
User newU = user;
return SUCCESS;
}
}
答案 0 :(得分:0)
我写的ajax语法错了。它必须像下面那样。
let email = $("#email").val();
let fName = $("#fName").val();
let lName = $("#lName").val();
let password = $("#pwd").val();
alert(email+" "+fName+" "+lName+" "+password);
var newUser = JSON.stringify({userList:new user(email,fName,lName,password)});
$.ajax({
type: 'GET',
url:'register.action',
data : {userList : newUser},
contentType:"application/json",
dataType:"json",
success: function(data){
}
});
我的struts.xml应该包含param标签,如下所示。
<action name="register" class="com.blogger.action.LoginAction" method="register">
<param name="userList">userList</param>
<result name= "success" type="json"></result>
</action>