我是靠谱客的博主 唠叨枫叶,这篇文章主要介绍Struts2 入门学习总结一,现在分享给大家,希望可以做个参考。

一、Struts2简介

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts2是Struts的下一代产品,这个框架充分发挥了Struts1和WebWork这两种技术的优势,抛弃原来Struts1的缺点,使得Web开发更加容易。

struts2还有以下优点:

  • 项目开源,使用及扩展方便 - 天生优势;
  • 提供Exception处理机制;
  • Result 方式的页面导航,通过 Result 标签很方便的实现重定向和页面跳转;
  • 通过简单、集中的配置来调度业务类,使得配置和修改都非常容易;
  • 提供简单、统一的表达式语言来访问所有可供访问的数据;
  • 提供标准、强大的验证框架和国际化框架;
  • 提供强大的、可以有效减少页面代码的标签;
  • 提供良好的Ajax支持;
  • 拥有简单的插件,只需要放入响应的 jar 包,任何人都可以扩展 Struts2 框架。

二、Struts2的工作目录结构和必须Jar包(使用的版本为2.5.17)

apps:示例存放的目录

docs:文档存放的目录

lib:jar包存放的目录

src:源码存放的目录

三、入门案例

1、创建工程

打开Eclipse,创建Dynamic Web Project

选择Next,勾选Generate web.xml deployment descriptor,创建web.xml文件

2、导入Struts2的必须Jar包

将Struts2的必须Jar包导入工程目录的WebContent目录下WEB-INF目录下的lib文件夹中

3、配置Struts2核心过滤器

在web.xml文件中进行如下配置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>ExampleProject</display-name> <!--配置Struts2核心拦截器 --> <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>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> </web-app>

Struts的核心控制器默认拦截以下请求

  • .action为后缀的请求
  • 没有任何后缀的请求

3、编写doAction

复制代码
1
2
3
4
5
6
public class doAction { public String sayHello() { System.out.printf("hello"); return "success"; } }

Action中处理请求的方法(动作方法)一般有以下特点

  • 访问修饰符均为public
  • 方法的返回值一般为String(可以为void)
  • 方法均没有参数

4、创建并配置struts.xml文件

在工程目录下的Java Resource目录下的src目录下创建struts.xml文件

在struts.xml文件中进行如下配置

5、创建index.jsp和hello.jsp

在工程目录下的WebContent目录下的WEB-INF文件夹下创建index.jsp和hello.jsp文件

index.jsp内容如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="${pageContext.request.contextPath}/hello">访问Struts示例程序</a> </body> </html>

hello.jsp内容如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>执行成功!</title> </head> <body> hello,world! </body> </html>

6、在Tomcat服务器上运行该工程

点击超链接转到hello.jsp页面

四、Struts2的工作流程图

以下是Struts2的工作流程图

  1. 客户端(Client)向Action发用一个请求(Request) 
  2. 容器(Container)通过web.xml映射请求,并获取核心过滤器(StrutsPrepareAndExecuteFilter或FilterDispatcher)在Struts2.1以前调用FilterDispatcher,Struts2.1以后调用StrutsPrepareAndExecuteFilter 
  3. 容器(Container)调用核心过滤器。核心过滤器(Controller)通过动作映射(ActionMapper)获得Action的信息 
  4. 然后核心过滤器(Controller)再调用动作类的代理类(ActionProxy ),由ActionProxy读取struts.xml文件获取Action和拦截器栈(Interceptor stack)的配置信息。 
  5. ActionProxy把Request请求传递给ActionInvocation 
  6. ActionInvocation依次调用Action和拦截器(Interceptor) ,再根据Action的配置信息生成Result 
  7. Result信息返回给ActionInvocation ,产生一个HttpServletResponse响应 
  8. 产生的HttpServletResponse响应 发送给客户端,客户端上展示响应内容。 

以上为Struts2框架的部分学习总结。

最后

以上就是唠叨枫叶最近收集整理的关于Struts2 入门学习总结一的全部内容,更多相关Struts2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部