struts學習

 

      如下是本人對struts的簡單理解,只學了兩天的小體會想記錄下!java

關於struts本人的簡單理解:web

     咱們先來看下struts的apache

       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" version="3.0">
  <display-name>Struts2</display-name>
     <filer>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<filter-mapping>
<filter-name>struts2</filter-name>


<url-pattern>*.action</url-pattern>


</filter-mapping>


</filter>
</web-app>


首先:  咱們得明白web.xml文件是用來幹啥的,  web.xml是用來配置一個filter(過濾器),讓struts框架可以來處理你以後的內容。app

 

  接下來咱們看看struts的框架

 

     struts.xml的文件jsp

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="defualt" extends="struts-defualt">
<action name="struts2" class="org.apache.StrutsAction">
<result name="success">success.jsp</result>
    <result name="error">error.jsp</result>
</action>
</package>
 </struts>


接下來咱們得知道struts.xml文件幹了啥事情,struts.jsp是用來肯定你須要調用那個Action來處理你的請求的

廢話了這麼多,下面就繼續廢話一些:下面咱們分析一下struts框架到底作了什麼工做

簡單點,說話的方式簡單點  在用戶頁面好比一個簡單的jsp頁面中表單的<form action="struts2.action">,在跳到struts2.action中的這個請求中url

首先會去到web.xml中註冊filter(過濾器)讓struts這個框架可以處理你的請求,在filter中的<filtel-class="org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter">spa

的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter(數據處理中心)處理,若是你提交的表單如是.action結尾的,就會被struts2的框架處理,*.action中的*表示code

的是你須要調用的名字。好比一個簡單的jsp頁面中表單的<form action="struts2.action">,.action前面的*是struts2,這說明struts2會被strut2框架調用。

 

web.xml發揮功力後,就輪到了struts.xml發揮做用了,struts.xml會映射決定用戶會調用哪一個Action對應在struts.xml配置是:

<action name="struts2" class="org.apache.StrutsAction">

以及決定映射到什麼頁面對應在struts.xml配置是:

    <result name="success">success.jsp</result>

    <result name="error">error.jsp</result>

我都不敢相信我竟然寫到這裏,你也竟然看到這裏!

     其實前面都是一些雜雜  簡單說,頁面-->控制器--->頁面

下面咱們來形象化一點的話:張三(至關於來自瀏覽器一個請求)問我(web.xml)要一個門票(使用struts框架),去博物館(struts.xml)看展品(Action)

相關文章
相關標籤/搜索