java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)

1.struts2快速配置:

  A.到http://struts.apache.org下载struts2开发包struts-2.3.32-all.zip

  java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)

  B.新建web项目并添加struts2依赖的jar文件

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)
  C.在web.xml配置struts2核心控制器(核心过滤器)

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)
  D.在src下新建struts2的配置文件:struts.xml,并引入dtd(struts2-core-2.3.32.jar--àstruts-2.3.dtd)

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)
  E.编写业务逻辑Action(LoginAction)

    

package cn.zzsxt.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
//属性驱动:获取页面提交参数的值
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;
} @Override
public String execute() throws Exception {
if("admin".equals(userName)&&"admin".equals(password)){
return "success";
}
return "fail";
}
}

  F.编写登陆页面login.jsp和登陆成功页面index.jsp

  login.jsp
<form action="login.action" method="post">
用户名:<input type="text" name="userName"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="登录"/>
</form> index.jsp
<html>
<head>
<title>登陆成功</title>
</head> <body>
恭喜你,${userName}登陆成功!
</body>
</html>

  G.在struts.xml配置action和result

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- package包:其作用类似java中包的概念,作用为:分类和避免命名冲突 -->
<package name="hello" extends="struts-default">
<action name="login" class="cn.zzsxt.action.LoginAction">
<result name="success">/index.jsp</result>
<result name="fail">/login.jsp</result>
</action>
</package>
</struts>

 2.关系:

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)

3.基础代码简化:

java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)

  struts.xml:

  

<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="cn.zzsxt.mystruts.action.LoginAction">
<result name="success">/index.jsp</result>
<result name="fail">/login.jsp</result>
</action>
<action name="add" class="cn.zzsxt.mystruts.action.AddAction">
<result name="success">/success.jsp</result>
<result name="fail">/add.jsp</result>
</action>
</struts>

  XML解析:

package cn.zzsxt.mystruts.framework;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class ActionMappingManager {
//封装struts.xml中所有的action节点信息,以action的name属性值做键,以action节点的信息做值
private Map<String,ActionMapping> actionsMap = new HashMap<String,ActionMapping>();
/**
* 解析src下的struts.xml配置文件
* 将action节点的信息封装成ActionMapping对象
* 将result节点信息封装成ResultMapping对象
*/
public void paserXml(){
SAXReader saxReader = new SAXReader();
//查找具有给定名称的资源,将src下的struts.xml转成输入流
InputStream ips =this.getClass().getResourceAsStream("/struts.xml");
try {
Document document = saxReader.read(ips);
//获取根节点
Element struts = document.getRootElement();
Iterator<Element> actionIter = struts.elementIterator();//获取子元素
while(actionIter.hasNext()){
Element action = actionIter.next();//action节点
String actionName = action.attributeValue("name");//获取action节点的name属性值
String actionClass = action.attributeValue("class");//获取action节点的class属性值
ActionMapping actionMapping = new ActionMapping(actionName,actionClass);//将name属性值和class属性值封装成actionMapping对象
Iterator<Element> resultIter = action.elementIterator();//获取result节点迭代器
while(resultIter.hasNext()){
Element result = resultIter.next();//获取result节点
String resultName = result.attributeValue("name");//获取result节点的name属性值
String resultPath = result.getText();//获取result节点的文本节点值
ResultMapping resultMapping = new ResultMapping(resultName,resultPath);//将Result节点信息封装成ResultMapping对象
actionMapping.getResultsList().add(resultMapping);//将resultMapping对象添加到actionMapping的集合中
}
//将action的节点信息添加到actionsMap中
actionsMap.put(actionName, actionMapping);
}
} catch (DocumentException e) {
e.printStackTrace();
}
} /**
* 根据action的name值查找action节点信息
* @param actionName
* @return
*/
public ActionMapping getActionByName(String actionName){
return actionsMap.get(actionName);
} public static void main(String[] args) {
ActionMappingManager actionMappingManager = new ActionMappingManager();
actionMappingManager.paserXml();//解析配置文件
ActionMapping actionMapping = actionMappingManager.getActionByName("login");
String actionName = actionMapping.getActionName();
String actionClass = actionMapping.getActionClass();
System.out.println(actionName+"----"+actionClass);
List<ResultMapping> list = actionMapping.getResultsList();
for (ResultMapping resultMapping : list) {
String resultName = resultMapping.getResultName();
String resultPath = resultMapping.getResultPath();
System.out.println(resultName+"****"+resultPath);
}
}
}

  ActionServlet:

package cn.zzsxt.mystruts.framework;

import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 1.核心控制器:拦截所有以.action结尾的用户请求,对地址栏进行截取
* http://localhost:8080/mystruts/login.action--->login
* 2.根据截取后的结果(login)到struts.xml配置文件中查找与其同名的action节点信息
* 3.获取action节点的class属性值(cn.zzsxt.mystruts.action.LoginAction),通过反射机制动态创建对象
* 4.利用多态执行action中的execute方法,返回逻辑名称(success/fail)
* 5.根据逻辑名称在struts.xml中的result节点查找同名的result节点信息
* 6.从result节点中获取文本节 点值(真实需要调整的路径),进行统一跳转
* @author Think
*
*/
public class ActionServlet extends HttpServlet {
ActionMappingManager actionMappingManager=null;
@Override
public void init() throws ServletException {
actionMappingManager = new ActionMappingManager();
actionMappingManager.paserXml();//解析配置文件struts.xml
} @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户请求的地址栏
String uri = request.getRequestURI();// /mystruts/login.action
String actionName=uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf("."));
System.out.println(actionName);
//根据截取后的结果从配置文件中查找对应的action节点信息
ActionMapping actionMapping = actionMappingManager.getActionByName(actionName);
//获取action节点的class属性值
String actionClass = actionMapping.getActionClass();
try {
//通过反射机制动态创建Action
Action action = (Action)Class.forName(actionClass).newInstance();
//执行action中execute方法,返回逻辑名称
String logicName = action.execute(request, response);
//根据逻辑名称查找配置文件,获取对于的result节点
List<ResultMapping> resultMappingList = actionMapping.getResultsList();//获取action的所有的result节点
for (ResultMapping resultMapping : resultMappingList) {
String resultName = resultMapping.getResultName();
String resultPath = resultMapping.getResultPath();//真实路径
if(resultName.equals(logicName)){
//跳转
request.getRequestDispatcher(resultPath).forward(request, response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} }

  Action接口:

package cn.zzsxt.mystruts.framework;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public interface Action {
public String execute(HttpServletRequest request,HttpServletResponse response);
}

  ActionMapping对象:

package cn.zzsxt.mystruts.framework;

import java.util.ArrayList;
import java.util.List; /**
* 封装struts.xml配置文件中的action节点的信息
* @author Think
*
*/
public class ActionMapping {
private String actionName;//封装action节点的name属性值
private String actionClass;//封装action节点的class属性值
private List<ResultMapping> resultsList = new ArrayList<ResultMapping>();//封装了该action节点的所有result节点的信息 public ActionMapping(String actionName, String actionClass) {
this.actionName = actionName;
this.actionClass = actionClass;
} public ActionMapping(String actionName, String actionClass, List<ResultMapping> resultsList) {
this.actionName = actionName;
this.actionClass = actionClass;
this.resultsList = resultsList;
} public ActionMapping() { } public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getActionClass() {
return actionClass;
}
public void setActionClass(String actionClass) {
this.actionClass = actionClass;
}
public List<ResultMapping> getResultsList() {
return resultsList;
}
public void setResultsList(List<ResultMapping> resultsList) {
this.resultsList = resultsList;
} }

  ResultSet对象:

package cn.zzsxt.mystruts.framework;
/**
* 封装struts.xml中result节点的信息
* @author Think
*
*/
public class ResultMapping {
private String resultName;//封装result节点的name属性值
private String resultPath;//封装result节点的文本节点的值 public ResultMapping(String resultName, String resultPath) {
this.resultName = resultName;
this.resultPath = resultPath;
} public ResultMapping() { } public String getResultName() {
return resultName;
}
public void setResultName(String resultName) {
this.resultName = resultName;
}
public String getResultPath() {
return resultPath;
}
public void setResultPath(String resultPath) {
this.resultPath = resultPath;
} }

   AddAction:

package cn.zzsxt.mystruts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import cn.zzsxt.mystruts.framework.Action; public class AddAction implements Action { @Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
//利用jdbc执行用户新增
System.out.println("执行了用户新增:"+userName+"---"+password);
return "success";//逻辑名称
} }

  LoginAction:

package cn.zzsxt.mystruts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import cn.zzsxt.mystruts.framework.Action; public class LoginAction implements Action { @Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if("admin".equals(userName)&&"admin".equals(password)){
return "success";//逻辑名称
}
return "fail";//逻辑名称
} }

  web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>mystruts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 核心控制器 -->
<servlet>
<servlet-name>actionservlet</servlet-name>
<servlet-class>cn.zzsxt.mystruts.framework.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>actionservlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

 

4.XML中的通配符:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- package包:其作用类似java中包的概念,作用为:分类和避免命名冲突 -->
<package name="hello" extends="struts-default">
<action name="login" class="cn.zzsxt.action.LoginAction">
<result name="success">/index.jsp</result>
<result name="fail">/login.jsp</result>
</action>
<action name="add" class="cn.zzsxt.action.AddAction">
<result name="success">/success.jsp</result>
</action>
</package>
<!-- 通过method指定action中默认方法 -->
<package name="hello2" extends="struts-default">
<!-- method="制定默认调用的方法,如果不指定默认调用execute方法" -->
<action name="user-doList" class="cn.zzsxt.action2.UserAction" method="doList">
<result name="list">/list2.jsp</result>
</action>
<!-- method="制定默认调用的方法,如果不指定默认调用execute方法" -->
<action name="user-doAdd" class="cn.zzsxt.action2.UserAction" method="doAdd">
<result name="success">/list.jsp</result>
</action>
</package> <!-- 方法的动态调用(重要):利用*作为通配符,通过{n} n代表第几个通配符,必须为正整数并且从1开始 -->
<!-- user-doLogin *匹配的是doLogin, method="{1}" {1}代表第一个通配符:doLogin -->
<package name="hello3" extends="struts-default">
<!-- method="制定默认调用的方法,如果不指定默认调用execute方法" -->
<action name="user3-*" class="cn.zzsxt.action3.UserAction" method="{1}">
<result name="list">/list3.jsp</result>
<!-- type="结果类型"
dispatcher默认(请求转发)
redirectAction:重定向到action
redirect:重定向到结果视图(jsp)
-->
<result name="success" type="redirectAction">user3-doList</result>
</action>
</package>
</struts>
上一篇:Struts2 Action中动态方法调用、通配符的使用


下一篇:【转】MySQL索引背后的数据结构及算法原理