日期:2014-05-16  浏览次数:20380 次

一个struts2+json+jquery的问题
问题比较简单,但是错乱的神经让我浪费啦一早晨.

1:配置环境

struts2+json+jquery

将jar包和js库引进.

2:配置文件:

web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


3:后台处理,实体和action
package com.sgai.jquery.vo;

/**
 * 用户实体类
 * 
 * @author 王俊磊
 *
 */
public class User {
	//登陆用户名
	private String loginName;
	//登陆账号
	private String password;
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

package com.sgai.jquery.web;

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

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.sgai.jquery.vo.User;

/**
 * 用户action层
 * 
 * @author 王俊磊
 *
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
	private User user;
	private String result;


	public String login() throws Exception {
		System.out.println("in");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", user.getLoginName());
		map.put("password",user.getPassword());
			result = JSONObject.fromObject(map).toString();
		
		return SUCCESS;
	}


	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
}


4:struts2配置文件

<?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>
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
	<constant name="struts.devMode" value="true"/>
	<constant name="struts.i18n.reload" value="true"/>
	<constant name="struts.configuration.xml.reload" value="true"/>
	
	<package name="login" namespace="/" extends="json-default" >
    	<action name="login" class="com.sgai.jquery.web.UserAction" method="login" >
    		<result type="json">
    			<param name="root">result</param>
    		</result>
    	</action>
</package> 
</struts>


5:前台请求页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title><s:text name="登陆" />
		</title>
		<script type="text/javascript" src="./jquery-1.6.1.js"></script>
		<script type="text/javascript" language="javascript">
	$(document).ready(function