[Struts2学习笔记]知识点:架构、生命周期、组件、配置文件

Struts2 MVC架构:
**模型:**属于软件设计模式的底层基础,主要负责数据维护。管理应用程序的数据,它通过响应视图的请求和控制器的指令来更新自身的数据。  
视图:这部分是负责向用户呈现全部或部分数据。通过控制器的指令触发所展现的一种特殊的数据格式。它们是基于像JSP、ASP、PHP之类模板系统的脚本,较易与AJAX技术进行整合。 
**控制器:**通过软件代码控制模型和视图之间的交互。控制器负责响应用户输入并执行数据模型对象的交互。控制器在接收、确认输入后执行修改数据模型状态的业务操作。
Struts2 架构五个核心部分:
操作(Actions)
拦截器(Interceptors)
值栈(Value Stack)/OGNL
结果(Result)/结果类型
视图技术
Struts2请求生命周期
用户发送一个资源需求的请求到服务器(例如:页面)。
核心控制器查看请求后确定适当的动作。
使用验证、文件上传等配置拦截器功能。
执行选择的动作来完成请求的操作。
另外,如果需要的话,配置的拦截器可做任何后期处理。
最后,由视图显示结果并返回给用户。
Struts2项目构建的四个组件:
1.Action(操作)
创建一个动作类,包含完整的业务逻辑并控制用户、模型以及视图间的交互。
2 Interceptors(拦截器)
这是控制器的一部分,可依据需求创建拦截器,或使用现有的拦截器。
3 View(视图)
创建一个JSP与用户进行交互,获取输入并呈现最终信息。
4 Configuration Files(配置文件)
创建配置文件来连接动作、视图以及控制器,这些文件分别是struts.xml、web.xml以及struts.properties。
Struts2项目的3个配置文件:
1.Struts.xml

<?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>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
     
      <action name="hello" 
            class="cn.w3cschool.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

2.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" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   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>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

3.struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config><!--配置文件的根节点 -->

   <!-- ========== Form Bean Definitions ============ -->
   <form-beans><!--将ActionForm子类映射到name的位置-->
      <form-bean name="login" type="test.struts.LoginForm" />
   </form-beans>

   <!-- ========== Global Forward Definitions ========= -->
   <global-forwards><!--将你在webapp上的页面映射到name,你可以使用这个name来引用实际页面。这避免了对你网页上的URL进行硬编码 -->
   </global-forwards>

   <!-- ========== Action Mapping Definitions ======== -->
   <action-mappings><!--声明表单处理程序的地方,也被称为操作映射(action mappings) -->
      <action
         path="/login"
         type="test.struts.LoginAction" >

         <forward name="valid" path="/jsp/MainMenu.jsp" />
         <forward name="invalid" path="/jsp/LoginView.jsp" />
      </action>
   </action-mappings>

   <!-- ========== Controller Definitions ======== -->
   <controller <!--配置Struts的内部,在实际情况中很少使用 -->
      contentType="text/html;charset=UTF-8"
      debug="3"
      maxFileSize="1.618M"
      locale="true"
      nocache="true"/>

</struts-config>

4.struts.properties文件

### When set to true, Struts will act much more friendly for developers
struts.devMode = true

### Enables reloading of internationalization files
struts.i18n.reload = true

### Enables reloading of XML configuration files
struts.configuration.xml.reload = true

### Sets the port that the server is run on
struts.url.http.port = 8080

学习资料:参考Struts2教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值