服务器(Tomcat)启动时会加载并解析web.xml配置文件,找到struts2的核心控制器StrutsPrepareAndExecuteFilter(新),通过StrutsPrepareAndExecuteFilter加载struts.xml配置文件的相关信息。首先客户端传来一个请求,这个请求会被StrutsPrepareAndExecuteFilter接收,其实是StrutsPrepareAndExecuteFilter类实现了FIlter(过滤器),这个过滤器通过请求中的.action对请求路径进行拦截。有两种情况:

①请求路径中不存在.action,则直接将路径传给struts.xml配置文件中的action标签,然后与其name中的属性值进行匹配,匹配成功后,再执行相关的业务操作;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>index</title>
</head>
<body>
<!--请求路径-->
<a href="product-input">跳转到商品添加页面</a>
</body>
</html>
<?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 name="test" extends="struts-default">
       <!--与请求路径匹配的name属性值-->
       <action name="product-input">
       <result>WEB-INF/pages/input.jsp</result>
       </action>
    </package>
</struts>

②请求路径中存在.action,Filter拦截器会截取.ation之前的路径,然后传给struts.xml,进行如上操作。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>input</title>
</head>
<body>
    <center>
<!--请求路径-->
<form action="product-save.action" method="post"> 商品名称:<input type="text" name="productName"/><p> 商品描述:<input type="text" name="productDesc"/><p> 商品价格:<input type="text" name="productPrice"/><p> <input type="submit" value="添加商品"/> </form> </center> </body> </html>
<?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 name="test" extends="struts-default">
       <!--与请求路径匹配的name属性值-->
       <action name="product-save" class="com.qixin.entity.Product" method="save" >
       <result name="detail">WEB-INF/pages/detail.jsp</result>
       </action>
    </package>
    
</struts>
posted on 2018-07-13 19:58  一笑奈何顾影自怜  阅读(104)  评论(0)    收藏  举报