<?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>
<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>3、第三步,编写Action,自己写了两个Action,一个是返回JSON数据的Action,一个是普通页面的Action
普通页面:
package com.aisino.development;返回JSON数据页面
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";
}
}
}
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"%>其余两个页面(注:自己编写的,与JSON数据无关)
<%
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>
<%@ 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,结构