ssh+jquery整合问题

Roy_zdc Roy_zdc 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

异步请发送到后台,后台处理后,返回了,但前台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>")
}
});
});
});


...全文
评论 14
收藏 已收藏
分享
返回顶部
Roy_zdc
Web 开发 81,108 成员 341,721 内容