当前位置: 代码迷 >> JavaScript >> strust 2.18 运用json 学习
  详细解决方案

strust 2.18 运用json 学习

热度:314   发布时间:2012-10-06 17:34:01.0
strust 2.18 应用json 学习

1. ?struts2 必需包.

2.

json-lib-2.2-jdk15.jar

?

commons-beanutils.jar,

commons- lang.jar,

ezmorph.jar,

struts2-json.jar

这些包在strust2.18中都有.

?

实例:界面

?

<%@ page language="java" ?pageEncoding="GBK"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

??<head>

?

?? ?<title>My JSP 'index.jsp' starting page</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="js/jquery-1.4.2.min.js"></script>

<script type="text/javascript" src="js/json2.js"></script>

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<script type="text/javascript">

?? $(function(){

?? $("#login").click(function(){

?? login();

??

?? });

??

?? })

?

?? function login(){

?? var url="user_login.action";

?? var name=$("#name").attr("value");

?? var password=$("#password").attr("value");

?? var params={"user.name":name,"user.password":password};

??

?? $.getJSON(url,params,function callback(data){

?? var user=eval("("+data+")");

??

?? $("#result").each(function(){

?? $(this).html('welcome ,'+user.name);

?? });

?? });

??

?? }

?

?</script>

??</head>

?

??<body>&nbsp;?

?? ?This is my JSP page. <br>

?

?? ?<s:actionmessage/>

?

?? ?<form action="" id ="form">

?? ? 用户名:

?? ? <input type="text" name="user.name" id="name">

?? ? <br>

?? ? 密码:

?? ? <input type="password" name="user.password" id="password"/>

?? ? <br>

<input type="button" id="login" value="登录" />

? ?

?? ?</form>

?

?? ?<div id="result"></div>

?

??</body>

</html>

?

action ? ?代码

?

package com.tzp;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6412115797195077412L;
	
	private User user;
	private String result;
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	public String login() throws Exception
	{
		Map map=new HashMap();
		if(user!=null){
			map.put("name", user.getName());
			map.put("password",user.getPassword());
			System.out.println(map.get("name").toString());
			
			JSONObject obj=JSONObject.fromObject(map);
			result=obj.toString();
			
		}else{
			result="";
			
		}
		System.out.println(result);
		return SUCCESS;
	}
	
}

?

?

struts 配置

?

?写道
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



<struts>
<package name="tzp" extends="json-default">
<action name="user_*" class="com.tzp.UserAction" method="{1}">
<result type="json">
<param name="root">result</param>
</result>

</action>
</package>
</struts>
?

  相关解决方案