本文主要是介绍使用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"/> <s:property value="lastName"/>
</body>
</html>
这篇关于使用Struts 2 建立向导应用(Wizard)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!