Struts2--Helloworld

简介:

搭建Struts2环境时,我们一般需要做以下几个步骤的工作:

1、找到开发Struts2应用需要使用到的jar文件.
2、创建Web工程
3、在web.xml中加入Struts2 MVC框架启动配置
4、编写Struts2的配置文件

开发Struts2应用依赖的jar文件

大家可以到http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x- all.zip。下载完后解压文件,开发struts2应用需要依赖的jar文件在解压目录的lib文件夹下。不同的应用需要的JAR包是不同的。下面给 出了开发Struts 2程序最少需要的JAR。

struts2-core-2.x.x.jar :Struts 2框架的核心类库

xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建

ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性

freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写

commons-logging-1.x.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。

commons-fileupload-1.2.1.jar :文件上传组件,2.1.6版本后必须加入此文件

以上这些Jar文件拷贝 到Web项目的WEB-INF/lib目录中。

Struts2在web.xml中的启动配置

在struts1.x中, struts框架是通过Servlet启动的。在struts2中,struts框架是通过Filter启动的。在web.xml中的配置如下:

<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>

在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。

注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件

Struts2应用的配置文件

Struts2默认的配置文件为struts.xml ,该文件需要存放在src目录下,该文件的配置模版如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

</struts>

实战--helloworld

Struts2的每个请求需要实现以下内容:
JSP
Action
struts.xml中Action配置

1、新建项目12.01

2、在默认的配置文件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>
    <constant name="struts.devMode" value="true" />
    <package name="Hello_World_Struts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
        <action name="hello" class="com.wuyudong.helloworld.action.HelloWorldAction" method="execute">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

在struts2框架中使用包来管理Action,包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action。在实际应用中,我们应该把一组业务功能相关的Action放在同一个包下。

配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,他不对应java的类包,如果其他包要继承该包,必须通过该属性进行引 用。包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下Action的路径的一部分,如访问上面例子的Action,访问路径 为:/test/helloworld.action。 namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。

通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核 心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。

例子中使用到的com.wuyudong.helloworld.action.HelloWorldAction类如下:

package com.wuyudong.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wuyudong.helloworld.model.MessageStore;

public class HelloWorldAction extends ActionSupport {
    private static final long serialVersionUID = -4958566543551999157L;
    private MessageStore msgStore;
    @Override
    public String execute() throws Exception {
        msgStore = new MessageStore("HelloWorld!");
        return SUCCESS;
    }
    public MessageStore getMsgStore() {
        return msgStore;
    }
    public void setMsgStore(MessageStore msgStore) {
        this.msgStore = msgStore;
    }
}

例子中使用到的hello.jsp如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Hello World!</title>
</head>
<body>
    <h2>
        <s:property value="msgStore.message" />
    </h2>
</body>
</html>

在struts2中,访问struts2中action的URL路径由两部份组 成:包的命名空间+action的名称,例如访问本例子HelloWorldAction的URL路径为:/helloworld (注意:完整路径为:http://localhost:端口/内容路径/helloworld)。另外我们也可以加上.action后缀访问此 Action。

Action名称的搜索顺序

1.获得请求路径的URI,例如url是:http://server/struts2/path1/path2/path3/test.action

2.首先寻找namespace为/path1/path2/path3的package,如果不存在这个package则执行步骤3;如果存在这 个package,则在这个package中寻找名字为test的action,当在该package下寻找不到action 时就会直接跑到默认namaspace的package里面去寻找action(默认的命名空间为空字符串“” ) ,如果在默认namaspace的package里面还寻找不到该action,页面提示找不到action

3.寻找namespace为/path1/path2的package,如果不存在这个package,则转至步骤4;如果存在这个 package,则在这个package中寻找名字为test的action,当在该package中寻找不到action 时就会直接跑到默认namaspace的package里面去找名字为test的action ,在默认namaspace的package里面还寻找不到该action,页面提示找不到action

4.寻找namespace为/path1的package,如果不存在这个package则执行步骤5;如果存在这个package,则在这个 package中寻找名字为test的action,当在该package中寻找不到action 时就会直接跑到默认namaspace的package里面去找名字为test的action ,在默认namaspace的package里面还寻找不到该action,页面提示找不到action

5.寻找namespace为/的package,如果存在这个package,则在这个package中寻找名字为test的action, 当在package中寻找不到action或者不存在这个package时,都会去默认namaspace的package里面寻找action,如果还 是找不到,页面提示找不到action。

Action配置中的各项默认值

创建 Model类MessageStore

package com.wuyudong.helloworld.model;

public class MessageStore {
    private String message;
    public MessageStore(String msg){
        this.setMessage(msg);
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

创建Action类HelloWorldAction,充当Controller

package com.wuyudong.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wuyudong.helloworld.model.MessageStore;

public class HelloWorldAction extends ActionSupport {
    private static final long serialVersionUID = -4958566543551999157L;
    private MessageStore msgStore;
    @Override
    public String execute() throws Exception {
        msgStore = new MessageStore("HelloWorld!");
        return SUCCESS;
    }
    public MessageStore getMsgStore() {
        return msgStore;
    }
    public void setMsgStore(MessageStore msgStore) {
        this.msgStore = msgStore;
    }
}

配置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>12.01</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.ng.filter.StrutsPrepareAndExecuteFilter
         </filter-class>
    </filter>
    
    <filter-mapping>
      <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

运行后如图所示

 

目录
相关文章
|
存储 人工智能 弹性计算
WordPress AI助手操作
本文将介绍如何使用阿里云百炼平台创建知识库与AI助手应用,包括数据上传、模型配置、应用部署及资源清理等步骤,并详细说明了如何在Web页面集成AI助手悬浮框,实现智能对话功能。
897 5
|
8月前
|
机器学习/深度学习 人工智能 算法
生成式AI商业崛起:不是魔法,是你的未来合伙人
还觉得AI是科幻电影里的玩意儿?醒醒!它已经在帮你写代码、画图、写诗了。从图灵的纸面理论到今天能通过图灵测试,AI经历了什么?为什么突然就这么厉害了?答案是:数据+算力+模型,三兄弟终于凑齐了!学会驾驭它,别只会围观,这是你从AI用户变成AI价值创造者的关键一跃。 #人工智能 #生成式AI #商业策略 #数字化转型
352 4
|
9月前
|
人工智能 自然语言处理 安全
2025年12月,中国数字人平台介绍与技术评析及数字化应用指南
2025年,虚拟数字人技术深度融入各行业。本文从技术、场景、安全三大维度,构建十大评估标准,揭示如何甄选兼具实用性、合规性与商业价值的数字人解决方案,助力企业智能化转型。
|
8月前
|
监控 Windows
Windows笔记本打游戏卡顿掉帧?一招教你解决!
笔记本玩游戏卡顿?可能是显卡设置出了问题。本文分享如何通过NVIDIA控制面板强制使用高性能显卡,轻松将《原神》《星穹铁道》帧率稳定至60FPS,提升游戏体验。排查思路清晰,操作简单,值得一试。
2357 0
|
前端开发
Promise的then方法返回的新Promise对象有什么特点?
Promise的then方法返回的新Promise对象有什么特点?
732 2
|
数据采集 人工智能 边缘计算
爬虫IP代理效率优化:策略解析与实战案例
本文深入探讨了分布式爬虫中代理池效率优化的关键问题。首先分析了代理效率瓶颈的根源,包括不同类型代理的特点、连接耗时及IP失效问题。接着提出了六大核心优化策略:智能IP轮换矩阵、连接复用优化、动态指纹伪装、智能重试机制等,并结合电商价格监控、社交媒体舆情分析和金融数据抓取三个实战案例,展示了优化效果。同时建立了三维效率评估体系,从质量、成本和稳定性全面衡量性能。最后展望了AI驱动调度、边缘计算融合等未来演进方向,帮助爬虫系统实现从“暴力采集”到“智能获取”的进化,大幅提升效率并降低成本。
687 0
|
NoSQL 关系型数据库 MySQL
招行面试:高并发写,为什么不推荐关系数据?
资深架构师尼恩针对高并发场景下为何不推荐使用关系数据库进行数据写入进行了深入剖析。文章详细解释了关系数据库(如MySQL)在高并发写入时的性能瓶颈,包括存储机制和事务特性带来的开销,并对比了NoSQL数据库的优势。通过具体案例和理论分析,尼恩为读者提供了系统化的解答,帮助面试者更好地应对类似问题,提升技术实力。此外,尼恩还分享了多个高并发系统的解决方案及优化技巧,助力开发者在面试中脱颖而出。 文章链接:[原文链接](https://mp.weixin.qq.com/s/PKsa-7eZqXDg3tpgJKCAAw) 更多技术资料和面试宝典可关注【技术自由圈】获取。
|
人工智能 数据可视化 程序员
2024年值得推荐的4款免费且功能强大的在线文档工具
2024年值得推荐的4款免费且功能强大的在线文档工具
1233 4
2024年值得推荐的4款免费且功能强大的在线文档工具
|
JSON 前端开发 JavaScript
Vite 共享配置
本文介绍了 Vite 配置中的 `base`、`mode`、`plugins`、`resolve` 和 `css` 等关键配置项。`base` 用于设置公共基础路径,`mode` 指定环境模式,默认为 `development` 和 `production`。`plugins` 注册项目中使用的插件。`resolve` 包含别名配置、去重依赖项和模块入口字段等选项。`css` 配置 CSS 预处理器及其选项,如导入样式变量文件。
567 6
|
机器学习/深度学习 存储 缓存
ATB概念之:算子tiling
算子 tiling 是一种优化技术,用于提高大规模张量运算的计算效率。它通过将大任务分解为小块,优化内存使用、支持并行计算,并防止内存溢出。在ATB中,tiling data指kernel的分片参数,用于指导计算。ATB提供了三种 tiling data 搬移策略:整体搬移、多stream搬移及随kernel下发搬移,旨在优化内存拷贝任务,提高计算效率。

热门文章

最新文章