首页 > 代码库 > struts2 自定义表单

struts2 自定义表单

自定义表单一定会涉及<s:iterator/>迭代,一个复杂的自定义表单可能会嵌套n多层迭代。

比如一个自定义一个问卷调查页面涉及3个模型:一个Survey代表一个调查,一个Page代表一个页面,一个Question代表一个问题。每个问题中会包含不同的表单元素,就会涉及迭代。


3个模型类如下:

Survey

package com.atguigu.surveypark.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 调查类
 */
public class Survey {
	private Integer id;
	private String title = "未命名";
	private String preText = "上一步";
	private String nextText = "下一步";
	private String exitText = "退出";
	private String doneText = "完成";
	private Date createTime = new Date();
	
	//建立从Survey到User之间多对一关联关系
	private User user ;
	
	//建立从Survey到Page之间一对多关联关系
	private Set<Page> pages = new HashSet<Page>();

	public Set<Page> getPages() {
		return pages;
	}

	public void setPages(Set<Page> pages) {
		this.pages = pages;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPreText() {
		return preText;
	}

	public void setPreText(String preText) {
		this.preText = preText;
	}

	public String getNextText() {
		return nextText;
	}

	public void setNextText(String nextText) {
		this.nextText = nextText;
	}

	public String getExitText() {
		return exitText;
	}

	public void setExitText(String exitText) {
		this.exitText = exitText;
	}

	public String getDoneText() {
		return doneText;
	}

	public void setDoneText(String doneText) {
		this.doneText = doneText;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

}

Page

package com.atguigu.surveypark.model;

import java.util.HashSet;
import java.util.Set;

/**
 * 页面类
 */
public class Page {
	private Integer id;
	private String title = "未命名";
	private String description;

	//简历从Page到Survey之间多对一关联关系
	private Survey survey;

	//简历从Page到Question之间一对多关联关系
	private Set<Question> questions = new HashSet<>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Survey getSurvey() {
		return survey;
	}

	public void setSurvey(Survey survey) {
		this.survey = survey;
	}

	public Set<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(Set<Question> questions) {
		this.questions = questions;
	}

}

Question

package com.atguigu.surveypark.model;

/**
 * 问题类
 */
public class Question {
	//
	private Integer id;
	// 题型0-8
	private int questionType;
	//
	private String title;
	// 选项
	private String options;

	// 其他项
	private boolean other;

	// 其他项样式:0-无 1-文本框 2-下拉列表
	private int otherStyle;

	// 其他项下拉选项
	private String otherSelectOptions;

	// 矩阵式行标题集
	private String matrixRowTitles;

	// 矩阵式列标题集
	private String matrixColTitles;
	// 矩阵是下拉选项集
	private String matrixSelectOptions;

	//建立从Question到Page之间多对一关联关系
	private Page page;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public int getQuestionType() {
		return questionType;
	}

	public void setQuestionType(int questionType) {
		this.questionType = questionType;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public boolean isOther() {
		return other;
	}

	public void setOther(boolean other) {
		this.other = other;
	}

	public int getOtherStyle() {
		return otherStyle;
	}

	public void setOtherStyle(int otherStyle) {
		this.otherStyle = otherStyle;
	}

	public String getOtherSelectOptions() {
		return otherSelectOptions;
	}

	public void setOtherSelectOptions(String otherSelectOptions) {
		this.otherSelectOptions = otherSelectOptions;
	}

	public String getMatrixRowTitles() {
		return matrixRowTitles;
	}

	public void setMatrixRowTitles(String matrixRowTitles) {
		this.matrixRowTitles = matrixRowTitles;
	}

	public String getMatrixColTitles() {
		return matrixColTitles;
	}

	public void setMatrixColTitles(String matrixColTitles) {
		this.matrixColTitles = matrixColTitles;
	}

	public String getMatrixSelectOptions() {
		return matrixSelectOptions;
	}

	public void setMatrixSelectOptions(String matrixSelectOptions) {
		this.matrixSelectOptions = matrixSelectOptions;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}


一个自定义表单
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>设计调查</title>
		<link rel="stylesheet" type="text/css" href=http://www.mamicode.com/''>>

上一个页面引用的头部

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<div class="divOuterFrame">
	<div class="divInnerFrame">欢迎使用SurveyDoor调查系统!</div>
</div>
<div class="divWhiteLine"></div>
<div class="divNavigatorOuterFrame">
	<div class="divNavigatorInnerFrame">
		<s:a action="LoginAction_toLoginPage" namespace="/">[首页]</s:a> 
		<s:a action="SurveyAction_newSurvey" namespace="/">[新建调查]</s:a> 
		<s:a action="SurveyAction_mySurveys" namespace="/">[我的调查]</s:a> 
		[参与调查] 
		<s:a action="RegAction_toRegPage" namespace="/">[用户注册]</s:a> 
		[用户授权管理] 
		[角色管理] 
		[权限管理] 
		[日志管理] 
	</div>
</div>
<div class="divWhiteLine"></div>