Struts2学习—你好,Nice to meet you !
2016-12-27 16:03
363 查看
初识Struts2
一、struts2介绍
Apache Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架。它成功地结合了WebWork和Struts1.x两种Web框架。使用OGNL表达式和Struts2标签来解决应用程序数据。通过Struts2可以减少使用MVC模式开发Web应用程序时间。
二、第一个struts2程序
1.下载struts2相关jar包.
地址:http://struts.apache.org/
2.struts2引入最少包,如下(若缺少会报错)
3.实现Action类
package com.ittx.struts.action; import org.apache.log4j.Logger; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static Logger log = Logger.getLogger(UserAction.class); @Override public String execute() throws Exception { /* * 接收请求 * 执行业务处理 * 返回响应 */ log.debug("执行添加用户操作 >>>>>>"); return SUCCESS; } }
4.核心配置文件struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 所有匹配*.do的请求都由struts2处理 --> <!-- <constant name="struts.action.extension" value="do" /> --> <!-- 让struts2支持动态方法调用<default-action-ref name="index"/> --> <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"/> --> <!-- package提供了将多个Action组织为一个模块的方式, package的名字必须是唯一的. namespace:定义package命名空间 该命名空间影响到url的地址, 例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action --> <package name="student" namespace="/" extends="struts-default"> <action name="user" class="com.ittx.struts.action.UserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
5.配置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>Struts</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>success.jsp</welcome-file> </welcome-file-list> </web-app>
6. 执行请求
运行Tomcat服务器:三、struts2工作原理
1.体系结构:应用流程注解
一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:点击打开链接https://github.com/apache/struts):
1. 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2. 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 3. 接着StrutsPrepareAndExecuteFilter被调用,StrutsPrepareAndExecuteFilter询问ActionMapper来决定这个请是否需要调用某个Action 4. 如果ActionMapper决定需要调用某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy 5. ActionProxy通过Configuration Manager询问框架的配置文件struts.xml,找到需要调用的Action类 6. ActionProxy创建一个ActionInvocation的实例。 7. ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。 8. 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2 框架中继承的标签。在这个过程中需要涉及到ActionMapper
相关文章推荐
- Lesson Five Nice to meet you. 很高兴见到你.
- Lesson 5 Nice to meet you
- 新概念英语(1-5)Nice to meet you.
- [转载] New Concept English 1——Lesson 5 Nice to meet you
- Lesson 5 Nice to meet you
- Nice to meet you
- Lesson 3 Nice to meet you.
- Lesson 5 Nice to meet you
- Lesson 5 Nice to meet you.
- Nice to meet you, STL.
- [1] Unit 1 Hello! Nice to meet you!
- 【akMOOC】1-1Nice to meet you!
- 【新概念第一册】Lesson_5 Nice to meet you
- hello,everyone!Nice to meet you!
- Lesson 5 Nice to meet you
- Give your contacts a big smile when you meet them. Make them feel that you are really happy to meet them.
- 数组nice to see you again
- I Very glad to meet you
- [安卓学习小记]You need to use a Theme.AppCompat theme (or descendant) with this activity.
- very nice to have talked with you via phone