首页 > 代码库 > 暑期项目经验(五)--struts+ajax
暑期项目经验(五)--struts+ajax
struts+ajax
一、ajax知识点
二、struts+ajax
a. index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>StrutsAjaxJson Demo</title> <script type="text/javascript" src="js/jquery-1.4.3.js"></script> <script type="text/javascript"> function loadInfo1(){ $.ajax({ url:‘returnMsg.action‘, type:‘post‘, dataType:‘json‘, success:function (msg){ $("#info1").html("发送成功:" + msg); }, error: function (xhr){ $("#info1").html("抱歉!发送失败!错误:" + xhr.responseText); } }); } function loadInfo2(){ $.ajax({ url:‘returnUser.action‘, type:‘post‘, dataType:‘text‘, success:function (msg){ $("#info2").html("发送成功:" + msg); }, error: function (xhr){ $("#info2").html("抱歉!发送失败!错误:" + xhr.responseText); } }); } function loadInfo4(){ $.ajax({ url: ‘sendMessage.action‘, data: "teacherName="+obj1+"&theme="+obj2+"&content="+obj3, contentType: ‘application/json; charset=utf-8‘, type: ‘get‘, dataType: ‘text/html‘, success: function (data) { if (data) { document.getElementById("span_msg_tips").innerHTML = "<div class=‘center‘ style=‘padding:30px;‘><span class=‘red‘>您的短消息已发送成功!</span> <a href=http://www.mamicode.com/‘/msg/inbox‘>返回收信箱
b.userInfo.java
package com.pxj.entity;public class UserInfo { private int userId; private String userName; private String pwd; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
c.TestAction.java
package com.pxj.action;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.Action;import com.pxj.entity.UserInfo;@SuppressWarnings("unchecked")public class TestAction { private String msg; //使用json返回单个值 private UserInfo userInfo; //使用json返回对象 private List userInfoList; //使用json返回List对象 private String teacherName,theme,content; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public UserInfo getUserInfo() { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } public List getUserInfoList() { return userInfoList; } public void setUserInfoList(List userInfoList) { this.userInfoList = userInfoList; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public String getTheme() { return theme; } public void setTheme(String theme) { this.theme = theme; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String returnMsg(){ this.msg="成功返回单个值"; return Action.SUCCESS; } public String returnUser(){ userInfo = new UserInfo(); userInfo.setUserId(1001); userInfo.setUserName("刘栋"); userInfo.setPwd("123"); return Action.SUCCESS; } public String returnList(){ userInfoList = new ArrayList<UserInfo>(); UserInfo u1 = new UserInfo(); u1.setUserId(2001); u1.setUserName("张三"); u1.setPwd("123"); UserInfo u2 = new UserInfo(); u2.setUserId(2002); u2.setUserName("李四"); u2.setPwd("456"); userInfoList.add(u1); userInfoList.add(u2); return Action.SUCCESS; } public String sendMessage(){ System.out.println("teacherName--theme--content:"+teacherName+":"+theme+":"+content); return Action.SUCCESS; }}
d.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="default" namespace="/" extends="json-default"> <action name="returnMsg" class="com.pxj.action.TestAction" method="returnMsg"> <result name="success" type="json"> <param name="root">msg</param> </result> </action> <action name="returnUser" class="com.pxj.action.TestAction" method="returnUser"> <result name="success" type="json"> <param name="includeProperties">userInfo\.userId,userInfo\.userName,userInfo\.pwd</param> <!--输出UserInfo的所有属性 <param name="includeProperties">userInfo.*</param> --> </result> </action> <action name="returnList" class="com.pxj.action.TestAction" method="returnList"> <result name="success" type="json"> <param name="includeProperties">userInfoList\[\d+\]\.userName,userInfoList\[\d+\]\.pwd</param> </result> </action> <action name="sendMessage" class="com.pxj.action.TestAction" method="sendMessage"> <result name="success" type="json"></result> </action> </package></struts>
暑期项目经验(五)--struts+ajax
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。