您好,登錄后才能下訂單哦!
1.這個例子是簡單的JQuery提交的例子
首先是JSP頁面的代碼
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>JQuery測試頁面</title>
- <script type="text/javascript">
- function verify(){
- var jqueryObj = $("#username");
- //獲取節點的值
- var userName = jqueryObj.val();
- alert(userName);
- $.get("TestAction!test.action?name=" + userName,null,callback);
- }
- function callback(data){
- //alert("服務器返回值:"+data);
- var resultObj = $("#result");
- resultObj.html(data);
- }
- function postsubmit(){
- $.post("TestAction!test.action?name=" + $("#username").val(),null,callback);
- }
- function clean(){
- var resultObj = $("#result");
- resultObj.html("");
- }
- </script>
- </head>
- <body>
- <h2>JQuery練習</h2>
- <input id="username" name="username" value="請輸入..."></input>
- <input type="button" onclick="verify()" value="確定"/>
- <input type="button" onclick="postsubmit()" value="post提交"/>
- <input type="button" onclick="clean()" value="清除"/>
- <div id="result"/>
- </body>
- </html>
接著是action的代碼
- package com.xiaoqiang;
- import java.io.PrintWriter;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class TestAction extends ActionSupport {
- private String name;
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- return "test";
- }
- public void test() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("name:"+new String(name.getBytes("iso-8859-1"),"utf-8"));
- HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
- httpServletResponse.setContentType("text/html;charset=utf-8");
- PrintWriter pw = httpServletResponse.getWriter();
- pw.print("可以注冊");
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。