Struts2用AJAX實現JSON外掛程式的使用

來源:互聯網
上載者:User

標籤:ajax   json   非同步   struts   

AJAX的詳細描述就不做介紹了,大家基本都懂這個。

下面我們會通過兩個具體的案例,來做具體的實現:

案例一:案例描述:輸入使用者名稱,把使用者名稱通過AJAX非同步傳輸的方式發送至後台,判斷此使用者名稱是否存在。
第一步,我們需要匯入這三個jar包:commons-lang3-3.1.jar、struts2-json-plugin-2.1.8.1.jar、xwork-core-2.1.6.jar,然後還有Struts2的幾個基本jar包第二步,還要匯入jQuery的設定檔:jquery-1.8.0.min.js(PS:這裡需要注意的一點是:xwork-core-2.1.6的jar包必須是這個2.1.6左右版本的,不然會出現一個異常)這是這個案例的目錄檢視:因為MyEclipse的版本原因,會在jquery的設定檔前邊打了個叉,這個不用管。

第三步,我們就要開始具體的編碼了:首先,我們需要建立一個Demo1.jsp檔案,用來發送註冊資料(存放位置請看):
<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>      <title>註冊</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript" src="${pageContext.request.contextPath }/jquery/jquery-1.8.0.min.js"></script><script type="text/javascript">$(function(){  /* 這表示在頁面載入的時候自動調用裡邊的方法 */$("#username").blur(function(){ /* 這裡表示在id=username的文字框失去焦點時候調用function()方法 */$.post("${pageContext.request.contextPath}/checkUserName.action", //這是URL{"username": $("#username").val()},  //這是資料function(data){   //data是從後台返回回來的資料的集合if(data.isExist){$("#result").html("<font color='red'>使用者名稱已經存在</font>");}else{$("#result").html("<font color='green'>使用者名稱不存在</font>")}});//這是AJAX傳輸資料的方法$.post()})})</script>  </head>    <body>    <h1>判斷使用者是否存在</h1>    <form>    使用者名稱:<input type="text" name="username" id="username" /><span id="result"></span><br>    密碼:<input type="password" name="password" id="password" />    <input type="submit" value="註冊" />    </form>  </body></html></span>

接下來,我們需要配置struts.xml檔案:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>  <package name="myjson" extends="json-default" >   <!-- 這裡必須要繼承json-default類 --><action name="checkUserName" class="cn.action.CheckUserNameAction" method="execute"><result type="json"></result> <!-- 因為這裡不是用result返回具體的視圖,而是要返回json資料,所以這裡的type是json --></action></package></struts> </span>

最後,我們需要的就是要寫一個Action類了:CheckUserNameAction
<span style="font-size:14px;">package cn.action;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;public class CheckUserNameAction extends ActionSupport{private List<String> existName = new ArrayList<String>();public CheckUserNameAction(){//因為僅僅示範AJAX,所以這裡假設existName是資料庫使用者的集合,然後添加了兩個資料existName.add("zhangsan");existName.add("lisi");}@Overridepublic String execute() throws Exception {if(existName.contains(username)){//這個名字不能被註冊,因為資料庫有這個名字isExist = true;}else{//這個名字可以被註冊,因為資料庫沒有這個名字isExist = false;}return SUCCESS;}private String username;  //這裡用來接收前台傳來的資料public void setUsername(String username) {this.username = username;}private boolean isExist; public boolean getIsExist(){ //這裡用來返回使用者是否可以註冊return isExist;}}</span>

上邊已經做完了,這時候我們就需要驗證了:
輸入:http://localhost:8080/book/json/Demo1.jsp然後,在使用者名稱輸入:zhangsan,會出現的情況:
案例一介紹結束。


Struts2用AJAX實現JSON外掛程式的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.