81,092
社区成员
发帖
与我相关
我的任务
分享
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";
}
}
<?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>
<?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>
//向服务器发送表达数据
$("#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>")
}
});
});
});
<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>