Servlet是线程不安全的,Struts1是基于Servlet的框架

而Struts2是基于Filter的框架,解决了线程安全问题

因此Struts1和Struts2基本没有关系,只是创造者取名问题

接下来搭建并测试

下载Struts2:https://struts.apache.org/

解压后目录如下:

apps中的是示例、docs是文档、lib是类库、src是源码

导包不需要导入lib中全部的包,导入这些即可

简单写一个Action类:

package hello;

public class HelloAction {

    public String hello(){
System.out.println("hello world!");
return "success";
} }

导入dtd约束,这里可以省略,只是在编写配置文件的时候没有提示,全凭手打易出错

配置文件:struts.xml(为什么这样写先不做说明,这里只是搭建测试)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="hello" namespace="/hello" extends="struts-default" >
<action name="HelloAction" class="hello.HelloAction" method="hello" >
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>

下一步至关重要,配置web.xml:

由于Struts2是基于Filter的框架,所以必须配置web.xml:

  <!-- struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

一个jsp文件hello.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>
<h1>hello world!</h1>
</body>
</html>

到这里框架搭建完成!

部署到Tomcat服务器,本地测试:

并且在控制台打印hello world!

测试成功!!

流程分析:

1.浏览器中访问....../hello/HelloAction

2.请求交到web.xml的Struts2过滤器,匹配hello和HelloAction

3.过滤器去寻找核心配置文件,/hello匹配namespace属性成功,于是寻找HelloAction类

4.找到HelloAction类,创建出Action对象,调用类的hello方法,打印hello world,返回"success"

5.返回值找到result标签的name属性,匹配成功,转发到hello.jsp

6.最终前端页面显示如图

struts2框架学习笔记1:搭建测试的更多相关文章

  1. j2ee开发之struts2框架学习笔记

    Struts2框架技术重点笔记 1.Struts2 是在webwork基础上发展而来. 2.Struts2 不依赖struts API和 servlet API 3.Struts2提供了拦截器,表现层 ...

  2. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  3. Struts2框架学习笔记1

    1,框架概述 1.1,什么是框架(了解) 将一些重复性的代码进行封装,简化程序员的编程操作,可以使得程序员在编码中把更多的精力放到业务需求的分析和理解上面,相当于一个半成品软件. 1.2,三大框架(掌 ...

  4. struts2框架学习笔记2:配置详解

    核心配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC ...

  5. struts2框架学习笔记7:struts2标签

    三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...

  6. struts2框架学习笔记6:拦截器

    拦截器是Struts2实现功能的核心部分 拦截器的创建: 第一种: package interceptor; import com.opensymphony.xwork2.ActionInvocati ...

  7. struts2框架学习笔记5:OGNL表达式

    OGNL取值范围分两部分,root.Context两部分 可以放置任何对象作为ROOT,CONTEXT中必须是Map键值对 示例: 准备工作: public void fun1() throws Ex ...

  8. struts2框架学习笔记4:获取参数

    第一种参数获取方式: 编写一个前端页面,提交表单,做示例: <form action="${pageContext.request.contextPath}/Demo1Action&q ...

  9. struts2框架学习笔记3:获取servletAPI

    Struts2存在一个对象ActionContext(本质是Map),可以获得原生的request,response,ServletContext 还可以获得四大域对象(Map),以及param参数( ...

随机推荐

  1. Linux命令:builtin

    语法: builtin [shell-builtin [arg ...]] 说明: 明确告诉bash执行的是内建命令,而不是和内建命令同名的函数.这实际就是bash容许用户自定义和builtin命令同 ...

  2. 双网卡Iptables端口转发

    当前场景如下 跳板机A是双网卡,有一个内网IP和一个公网IP 内网IP: 10.0.10.30 外网IP:  58.68.255.123 内网机器:10.0.30.88,可以和10.0.10.30通讯 ...

  3. dto vo

    不过符合规矩的做法是DTO里可以放各种List<VO>,而VO和entity就是一一对应的关系,vo里不能放entity,entity里也不能放vo,vo和entity只存放和数据库完全相 ...

  4. js中的数据类型有

  5. Codeforces Round #438 A. Bark to Unlock

    题意:给你一个原串和n个子串,问你这n个子串任意组合起来能不能使原串出现,串的长度为2. Examples Input ya4ahoytoha Output YES Input hp2http Out ...

  6. Kafka自带zookeeper报错INFO Got user-level KeeperException when processing xxx Error Path:/brokers Error:KeeperErrorCode = NodeExists for /brokers (org.apache.zookeeper.server.PrepRequestProcessor)

    问题描述: 按照kafka官方文档的操作步骤,解压kafka压缩包后.依次启动zookeeper,和kafka服务 kafka服务启动后,查看到zookeeper日志里有以下异常 问题原因及解决办法: ...

  7. linux百万并发之 tcp_mem

    百万并发之 tcp_mem   在服务端,连接达到一定数量,诸如50W时,有些隐藏很深的问题,就不断的抛出来. 通过查看dmesg命令查看,发现大量TCP: too many of orphaned ...

  8. 安全运维之:Linux后门入侵检测工具的使用

    安全运维之:Linux后门入侵检测工具的使用 https://blog.csdn.net/exitgogo/article/details/39547113

  9. 相机标定——OpenCV-Python Tutorials

    原文地址http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_calib3d/py_calibration/py ...

  10. jdk1.8 HashMap的实现

    在了解HashMap之前,我们先进行位运算知识的补充 1.Java 位运算:(都是二进制的运算) << :相当于乘以2的倍数  --->1<<4  =1*2*2*2*2 ...