日期:2014-05-16  浏览次数:20597 次

Struts2 Jquery+Ajax提交数据到后台
//UpdateAction.java



package com.abin.hospital.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.abin.hospital.bean.UserBean;
import com.abin.hospital.singleton.UpdateSingleton;
import com.abin.hospital.singleton.UserSingleton;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateAction extends ActionSupport{
private static final UpdateSingleton updateSinglenton=UpdateSingleton.getInstance();
private String result;

public String update(){
  ActionContext context=ActionContext.getContext();
  HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  String password1=request.getParameter("password1");
  String password2=request.getParameter("password2");
  UserBean bean=null;
  try {
   bean=this.updateSinglenton.findUserByUserID(username);
  } catch (Exception e) {
   e.printStackTrace();
  }
 
  if(bean==null||bean.getUserid()==null){
   result="notexist";
   return "json";
  }
 
  if(bean.getUserid()!=null&&!bean.getPassword().equals(password)){
   result="failure";
   return "json";
  }
 
  if(bean.getUserid()!=null&&bean.getPassword().equals(password)){
   int end=0;
   try {
    end=this.updateSinglenton.updateUserByUserID(bean,password1);
   } catch (Exception e) {
    e.printStackTrace();
   }
   if(end==1){
    result="success";
    return "json";
   }else{
    result="updatefailure";
    return "json";
   }
  
  }
 
  return null;
}

public String getResult() {
  return result;
}

public void setResult(String result) {
  this.result = result;
}
}




//struts-update.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>
<package name="userupdate" extends="json-default" namespace="/user" >

  <action name="update" method="update" class="com.abin.hospital.action.UpdateAction">
   <result name="json" type="json"></result>
  </action>

</package>


</struts>





//update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Young ABIN</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
</head>
<script  type="text/javascript" src="../js/jquery-1.7.min.js"></script>
<script  type="text/javascript">
function Validate() {
  if($("#user").val(