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










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值