利用Struts2框架,将后台数据转化为JSON数据并返回到前台

1、第一步,配置web.xml,配置代码如下


	
  
	
	struts2
	
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



	struts2  
	/*  



    login.jsp


2、配置Struts2,注意,配置一般数据时与配置JSON数据时的配置文件不同


  
    
    
    
    	
    	
     	
    			/JSP/success.jsp
    			/JSP/fail.jsp    			
    	
    	

	//返回JSON数据的配置
    	
    	
		   			
		   				list
		   			
		   	
    	
    
3、第三步,编写Action,自己写了两个Action,一个是返回JSON数据的Action,一个是普通页面的Action

     普通页面:

package com.aisino.development;

import com.opensymphony.xwork2.ActionSupport;

public class login extends ActionSupport{
	private String username;
	private String password;
	
	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 String execute() {
		System.out.println("aaa");
		if("admin".equals(this.username) && "123".equals(password)){
			return "success";
		}
		else{
			return "fail";
		}

	}
	
}
 返回JSON数据页面

  

package com.aisino.development;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class UserTestAction extends ActionSupport {
	
	private List list;
	
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	//这里所需要的Users类已经定义,定义List集合,将信息存储到集合中
	public String Test(){
		Users users=new Users();
		users.setId(11);
		users.setName("张三丰");
		users.setUsername("张三");
		users.setPassword("123456");
		
		list=new ArrayList();
		list.add(users);
		
		return SUCCESS;
	}

}
Users类

package com.aisino.development;

public class Users {
	private long id;
	private String name;
	private String username;
	private String password;
	//切记get、set方法
	public Users() {
		super();
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	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;
	}
}

4,jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    登录页面
	
	
	    
	
	
	
  
  
  
  

登录系统

账号:

密码:

  其余两个页面(注:自己编写的,与JSON数据无关)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    登录成功
    
	
	
	    
	
	
	

  
  
  
    账号密码正确 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    登录失败
    
	
	
	    
	
	
	

  
  
  
    账号或密码错误 
5、通过网页查找返回的JSON数据


6,结构




你可能感兴趣的:(利用Struts2框架,将后台数据转化为JSON数据并返回到前台)