当前位置: 代码迷 >> JavaScript >> struts2+jquery+ajax+json+struts2_json_plugin配备例子
  详细解决方案

struts2+jquery+ajax+json+struts2_json_plugin配备例子

热度:174   发布时间:2012-09-02 21:00:34.0
struts2+jquery+ajax+json+struts2_json_plugin配置例子

1.struts.xml

	<constant name="struts.i18n.encoding" value="utf-8" />

	<package name="jsondefault" namespace="/json" extends="json-default">
		<action name="loginByAjax" class="com.mtea.s2.action.LoginByAjax">
			<result type="json"/>
		</action>
	</package>

2.Action

package com.mtea.s2.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class LoginByAjax extends ActionSupport{
	private String username;
	private String password;
	private boolean pass;
	private String message;
	private List<String> fruitList=new ArrayList<String>();
	private Map<String,String> realNameMap=new HashMap<String,String>();
	private User user=new User("xiao7",22);
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isPass() {
		return pass;
	}
	public void setPass(boolean pass) {
		this.pass = pass;
	}
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<String> getFruitList() {
		return fruitList;
	}
	public void setFruitList(List<String> fruitList) {
		this.fruitList = fruitList;
	}
	public Map<String, String> getRealNameMap() {
		return realNameMap;
	}
	public void setRealNameMap(Map<String, String> realNameMap) {
		this.realNameMap = realNameMap;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public String execute() throws Exception{
		//init
		fruitList.add("苹果");
		fruitList.add("香蕉");
		fruitList.add("芒果");
		realNameMap.put("张", "三");
		realNameMap.put("李", "四");
		
		if (username.equals("macrotea") && password.equals("123456")) {
			pass = true;
			message = "用户名密码正确";
			System.out.println("message:" + message);
		} else {
			message = "用户名密码不正确";
			pass = false;
			System.out.println("message:" + message);
		}
		
		//只能返回success
		return Action.SUCCESS;
	}
/*
	//其实不用继承ActionSupport,不用重写execute()
	public String execute(){
		// do someting
	}
	这样也可以的,不过已经养成了手贱的习惯
*/
}

?3.jsp,通过ff3.6,ie7+

<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" session="true"%>
<%@ page import="java.text.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>set your title</title>
    <script src="/js/jquery-1.3.2.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#loginForm #submit").click(function(event){
			
			$("#loginForm").submit(function(event){
				
				//必须的,无论你是$("#loginForm #submit").click(function(event){
				//阻止默认行为再进行ajax提交
				event.preventDefault();
				$.ajax({
					   type: "POST",
					   //我是使用struts2+struts2-json-plugin-2.2.1.jar的,这些都不用设置
					   //contentType: "text/html;charset=utf-8",
					   //contentType: "application/json;charset=utf-8",
					   url: "/json/loginByAjax",
					   data: {
						   username:$("#username").val(),
						   password:$("#password").val()
					   },
					   success: function(data){
					     $("#result").html(data);
					   }
				});
			});
		});
	</script>
</head>

<body>
<div id="result"></div>
<form id="loginForm" method="post">
	<legend>登陆页</legend>
  	<div class="error"></div>
  <p>
    <label for="username">用户名</label>
    <br />
<input type="text" name="username" id="username" autocomplete='off' value="macrotea"/>
  </p>
  <p>
    <label for="password">密码</label>
    <br />
<input type="text" name="password" id="password" autocomplete='off' value="123456"/>
  </p>
  <p>
    <input type="submit" value="登陆" id="submit"/>
  </p>

</form>

</body>

</html>
  相关解决方案