利用Struts2框架,將後臺資料轉化為JSON資料並返回到前臺
阿新 • • 發佈:2019-02-09
1、第一步,配置web.xml,配置程式碼如下
4,jsp頁面
<?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"> <filter> <!--配置struts2過濾器 --> <!--過濾器名稱 --> <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> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
2、配置Struts2,注意,配置一般資料時與配置JSON資料時的配置檔案不同
<?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="default" namespace="/" extends="struts-default"> <!--定義action --> <action name="login" class="com.aisino.development.login" method="execute"> <result name="success">/JSP/success.jsp</result> <result name="fail">/JSP/fail.jsp</result> </action>
</package>
3、第三步,編寫Action,自己寫了兩個Action,一個是返回JSON資料的Action,一個是普通頁面的Action<span style="white-space:pre"> </span>//返回JSON資料的配置 <package name="json" extends="json-default" namespace="/"> <action name="UserTestAction" class="com.aisino.development.UserTestAction" method="Test"> <result name="success" type="json"> <param name="root">list</param> </result> </action> </package> </struts>
普通頁面:
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;
}
<span style="white-space:pre"> </span>//這裡所需要的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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登入頁面</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center><h2>登入系統</h2>
<form action="login" method="post">
賬號:<input type="text" name="username" /><br><br>
密碼:<input type="password" name="password"/><br><br>
<input type="submit" value="提交"/>
<input type="reset" value="重寫"/>
</form>
</center>
</script>
</body>
</html>
其餘兩個頁面(注:自己編寫的,與JSON資料無關)<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登入成功</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
賬號密碼正確 <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登入失敗</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
賬號或密碼錯誤 <br>
</body>
</html>
5、通過網頁查詢返回的JSON資料
6,結構