学习Struts--Chap01:了解Struts2

学习之前的一些话:

  这是我系统学习Java知识这么久以来第一次接触web框架,很不幸的是刚开始学习它就听到很多人说这个框架现在已经过时了,很多企业现在开发项目几乎不会用这个框架了,就是有用这个框架的项目也是以前的旧项目,因为前边被爆出来存在几处高危漏洞(已经修复),这些高危漏洞对项目系统存在很大的安全隐患,导致失去一大波用户,慢慢也就成为人们口中的即将过时淘汰的框架。软件开发技术的更新速度本来就比较快,但是我所接触的人都告诉我学习Java虽然看着很简单,有很多已经封装好的东西直接拿来用就好了,实话说真正理解它的底层实现原理还是非常困难的,人要不停的进步,才能不会被取代,仅仅会写代码并不能在这个行业长远的走下去,要不断的学习不断地增加自己的知识储备,所以这个框架虽然逐渐被淘汰,但是我既然要学习它就要学的比较深入,因为这不单单是学的好与坏的问题,还会让自己对Java有更深入的理解,为长远发展打下基础。

先写一个小的demo,保证能运行起来:使用struts2输出hello world

1、根据要求我们创建一个Dynamic web project(要求选Dynamic web module version的版本选2.5,因为这里需要用xml配置文件)。准备阶段因为我们要使用Struts2的框架,所以先把官网下载的jar包放到webContent-->WEB-INF-->lib下边。

 2、在xml文件中设置一个过滤器,要求过滤一切的请求,然后经过过滤器的请求被分发给不同的Action类处理。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>HeadFirstStruts2Chap01</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13     <filter>
14         <filter-name>Struts2</filter-name>
15         <filter-class>
16             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
17         </filter-class>
18     </filter>
19 
20     <filter-mapping>
21         <filter-name>Struts2</filter-name>
22         <url-pattern>/*</url-pattern>
23   </filter-mapping>
24 
25 
26 </web-app>

3、创建一个HelloWorldAction的类:这个类需要实现Action这个接口,然后重写它的一个名为execute的默认方法。

 1 package com.java1234.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 public class HelloWorldAction implements Action{
 6     //这里只是简单的实现Action这个类的功能
 7     @Override
 8     public String execute() throws Exception {
 9         System.out.println("执行的是Action的默认方法!");
10         return SUCCESS;
11     }
12 
13 }

4、在src的目录下创建一个名为struts.xml的配置文件。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <package name="helloWorld" extends="struts-default">
 9       <action name="hello" class="com.java1234.action.HelloWorldAction">
10           <result name="success">helloWorld.jsp</result>
11       </action>
12       
13  
14   </package>
15 
16 </struts>

5:创建一个result返回结果之后跳转的界面:helloWorld.jsp

<%@ 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>Insert title here</title>
</head>
<body>
  Hello,World(Struts2)你好!
</body>
</html>

6:浏览器运行实现:http://localhost:8080/HeadFirstStruts2Chap01/hello 就能得出结果。

 7:总结一下这个小程序的大致运行过程:我们在该项目的web.xml文件中配置了一个过滤器,这个过滤器作用就是过滤一切的请求,并把过滤的请求交给org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 这个类去处理,这个类的主要作用就是处理Struts调度过程的准备和执行阶段。然后根据请求得知访问的是hello。所以就通过查找src目录下创建的一个名为struts.xml的配置文件得知,这个请求交给com.java1234.action.HelloWorldAction 这个Action类(处理请求、封装数据)进行处理,然后和返回结果result中name名为success的result刚好相匹配(result标签可以写好多个),result默认的type类型是forward,然后就跳转到了helloWorld.jsp,这样就输出了helloWorld.jsp界面上的内容:Hello,World(Struts2)你好!

备注:struts.xml为Struts 2的核心应用配置文件。struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等。struts.xml中主要配置Struts项目的一些全局的属性,用户请求和响应Action之间的对应关系,以及配置Action中可能用到的参数,以及处理结果的返回页面。还包括各种拦截器的配置等。

关于Struts2的工作原理和工作流程的讲解请看:https://www.cnblogs.com/BaoZiY/p/10144481.html 

原文地址:https://www.cnblogs.com/BaoZiY/p/10140665.html