使用Struts 2 建立向导应用(Wizard)

2024-02-08 02:58

本文主要是介绍使用Struts 2 建立向导应用(Wizard),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

向导 Wizard指需要一系列选择的操作,每一个选择决定了后续的操作.
这里使用一个简单的向导的模型来说明如何使用Struts 2 建立此种类型的应用.

例如如下图所示:
流程图:
其中橙色部分表示了信息的内容
FirstName.jsp
LastName.jsp
ShowName.jsp
代码示例:
1. WizardAction 
package net.dev.java.teamware;

import com.opensymphony.xwork2.ActionSupport;

/**
* Created by IntelliJ IDEA.
* User: mazhao
* Date: 2008-5-5
* Time: 21:10:44
* To change this template use File | Settings | File Templates.
*/
public class WizardAction extends ActionSupport {
private String firstName;
private String lastName;
private String actionName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getActionName() {
return actionName;
}

public void setActionName(String actionName) {
this.actionName = actionName;
}


/**
*   FirstName   ->   LastName   ->   Show Name
*              next           submit
*   FirstName     <-    LastName
*              previous
*
*   -> FirstName
*   init
*
* @return
*/
 public String execute() {

        if ("next".equals(actionName)) {
            return "next";
        } else if ("previous".equals(actionName)) {
            return "previous";
        } else if ("submit".equals(actionName)) {
            return "submit";
        } else {
            return "init";
        }
    }
}

2. struts.xml 配置
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="myPackage" extends="struts-default">
<action name="index" class="net.dev.java.teamware.IndexAction">
<result>/jsp/index.jsp</result>
</action>
<action name="helloWorld" class="helloWorldAction"> 
<result name="input">/jsp/index.jsp</result>
<result>/jsp/helloWorld.jsp</result>
</action>

<action name="wizard" class="net.dev.java.teamware.WizardAction">
            <result name="init">/jsp/FirstName.jsp</result>
            <result name="next">/jsp/LastName.jsp</result>
            <result name="previous">/jsp/FirstName.jsp</result>
            <result name="submit">/jsp/ShowName.jsp</result>
        </action>
</package>
</struts>

3. JSP代码
FirstName.jsp
<%--
Created by IntelliJ IDEA.
User: mazhao
Date: 2008-5-5
Time: 21:13:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Simple jsp page</title></head>
<body>
Enter your first name here!
<br/>
<s:form action="wizard"  method="submit" >
<s:textfield name="firstName" label="First Name"/>
<s:submit label="Next" name="actionName" value="next"/>
</s:form>

</body>
</html>

LastName.jsp
<%--
Created by IntelliJ IDEA.
User: mazhao
Date: 2008-5-5
Time: 21:14:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<html>
<head><title>Simple jsp page</title></head>
<body>
Enter your last name here!
<s:form action="wizard" method="post">
<s:textfield name="firstName" label="First Name"/>
<s:textfield name="lastName" label="Last Name"/>
<s:submit label="Submit" name="actionName" value="submit"/>
<s:submit label="Previous" name="actionName" value="previous"/>
</s:form>
</body>
</html>

ShowName.jsp
<%--
Created by IntelliJ IDEA.
User: mazhao
Date: 2008-5-5
Time: 21:14:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<html>
<head><title>Simple jsp page</title></head>
<body>
The Name is :<br/>
<s:property value="firstName"/> &nbsp; <s:property value="lastName"/>
</body>
</html>










这篇关于使用Struts 2 建立向导应用(Wizard)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/689709

相关文章

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令