ssh+jquery整合问题

Roy_zdc 2011-08-16 09:21:34

异步请发送到后台,后台处理后,返回了,但前台Jquery的回调函数没有触发。

以下是我的相关配置文件和代码。


package com.gifer.web;

import java.util.List;

import com.gifer.model.LoginUser;
import com.gifer.service.LoginUserService;
import com.opensymphony.xwork2.ActionSupport;

/**
* @author zhongdc
*
*/
public class LoginUserAction extends ActionSupport {

private static final long serialVersionUID = 1127293641745744474L;

private LoginUserService userManager;

private LoginUser user;//单个用户对象
private List<LoginUser> userInfosList;//用户列表
private String result;//请求结果标识

public LoginUserService getUserManager() {
return userManager;
}
public void setUserManager(LoginUserService userManager) {
this.userManager = userManager;
}

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

public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public List<LoginUser> getUserInfosList() {
return userInfosList;
}
public void setUserInfosList(List<LoginUser> userInfosList) {
this.userInfosList = userInfosList;
}


public String returnListUser() throws Exception{
try {
this.userInfosList= this.userManager.findAll();
this.result="true";
} catch (Exception e) {
e.printStackTrace();
this.result="false";
}

return "ListUser";
}

public String returnSaveUser() throws Exception{
try {
this.userManager.save(user);
this.result="true";
} catch (Exception e) {
e.printStackTrace();
this.result="false";
}
return "SaveUser";
}

public String returnDeleteUser() throws Exception{
try {
this.userManager.delete(user);
this.result="true";
} catch (Exception e) {
this.result="false";
e.printStackTrace();
}
return "DeleteUser";
}

public String returnFindUser() throws Exception{
try {
this.user= this.userManager.findById(this.user.getUserId());
this.result="true";
} catch (Exception e) {
e.printStackTrace();
this.result="false";
}
return "FindUser";
}
}


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="com.gifer.web" namespace="/" extends="json-default">
<!-- 一个action接收多个请求的配置方法 -->
<action name="UserAction" class="UserManagerAction">
<result name="ListUser" type="json"></result>
<result name="SaveUser" type="json"></result>
<result name="DeleteUser" type="json"></result>
<result name="FindUser" type="json"></result>
</action>
</package>
</struts>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="gifer"></property>
<property name="password" value="gifer"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/gifer/model/LoginUser.hbm.xml</value>
</list>
</property>
</bean>
<bean id="LoginUserDAO" class="com.gifer.dao.LoginUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>

<bean id="LoginUserService" class="com.gifer.service.LoginUserService">
<property name="dao">
<ref bean="LoginUserDAO" />
</property>
</bean>

<bean id="UserManagerAction" class="com.gifer.web.LoginUserAction" scope="prototype">
<property name="userManager">
<ref bean="LoginUserService"/>
</property>
</bean>
</beans>


js

//向服务器发送表达数据
$("#regRe").click(function() {
//把表单的数据进行序列化
var params = $("form").serialize();
//使用jQuery中的$.ajax({});Ajax方法
$.ajax( {
url : "UserAction!returnSaveUser",//!前面是Action的名字,!后面是执行的方法名
type : "POST",
data : params,
dataType : "json",
success : function(data) {
if(data.result=="true"){
alert("注册成功!");
}
//清空显示层中的数据
$("#message").html("");
//为显示层添加获取到的数据
//获取对象的数据用data.user.属性
$("#message").append(
"<div><font color='red'>用户ID:"
+ data.user.userId
+ "</font></div>").append(
"<div><font color='red'>用户名:"
+ data.user.userName
+ "</font></div>").append(
"<div><font color='red'>密码:"
+ data.user.password
+ "</font></div>")
}
});
});
});


...全文
150 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
calmlifer 2011-08-16
  • 打赏
  • 举报
回复
来看看 学习学习。
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 softroad 的回复:]

XML code

<action name="*Action"

class="com.xuxinhua1984.struts2.i18n.LoginAction" method="{1}">

<result name="success">/success.jsp</result>

<result name="error">/error.jsp</result>

</action……
[/Quote]

我用的是异步请求,这两种方式应该是不行吧
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 softroad 的回复:]

url : "UserAction!returnSaveUser"
url : "UserAction!returnSaveUser.action"
[/Quote]

之前我spring注入有问题的时个,这两种方式,都是可以用的。
现在spring注入的问题解决了,上面这两种方式,请求都没有触发JQuery的回调函数。
softroad 2011-08-16
  • 打赏
  • 举报
回复

<action name="*Action"

class="com.xuxinhua1984.struts2.i18n.LoginAction" method="{1}">

<result name="success">/success.jsp</result>

<result name="error">/error.jsp</result>

</action>



<action name="loginMethod"

class="com.polaris.LoginAction" method="login">

<result>/result.jsp</result>

<result name="error">/error.jsp</result>

</action>

以上2种方法试试。
softroad 2011-08-16
  • 打赏
  • 举报
回复
url : "UserAction!returnSaveUser"
url : "UserAction!returnSaveUser.action"
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 greensurfer 的回复:]

引用 6 楼 softroad 的回复:

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


[/Quote]
我试过了,还是不行。
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 softroad 的回复:]

struts.xml
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
[/Quote]
我试试,这个代表什么意思呢?
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jnhcd 的回复:]

汗,没看到你的struts.xml。。囧rz
[/Quote]
上面贴出来了啊
softroad 2011-08-16
  • 打赏
  • 举报
回复
struts.xml
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jnhcd 的回复:]

我晕,你的returnSaveUser怎么还是String的返回类型啊,
应该用void,然后把想要的结果往response里面写,这样你的回调函数才能取到值
[/Quote]
没太明白你的意思,能否说具体点?
jnhcd 2011-08-16
  • 打赏
  • 举报
回复
汗,没看到你的struts.xml。。囧rz
jnhcd 2011-08-16
  • 打赏
  • 举报
回复
我晕,你的returnSaveUser怎么还是String的返回类型啊,
应该用void,然后把想要的结果往response里面写,这样你的回调函数才能取到值
Roy_zdc 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 softroad 的回复:]

struts.enable.DynamicMethodInvocation=true //设置为启用
[/Quote]

这个设在哪呢?
softroad 2011-08-16
  • 打赏
  • 举报
回复
struts.enable.DynamicMethodInvocation=true //设置为启用

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧