歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

簡單的jQuery提交例子

這個例子是簡單的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>
<scripttype="text/javascript"src="js/jquery-1.4.4.min.js"></script>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8">
<title>JQuery測試頁面</title>
<scripttype="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>
    <h1>JQuery練習</h1>
    <inputid="username"name="username"value="請輸入..."></input>
    <inputtype="button"onclick="verify()"value="確定"/>
    <inputtype="button"onclick="postsubmit()"value="post提交"/>
    <inputtype="button"onclick="clean()"value="清除"/>
   
    <divid="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;
    }
   
   
   
}

Copyright © Linux教程網 All Rights Reserved