struts2表單標籤實驗:個人信息表單

1) 新建一個PersonFormAction類,聲明username(姓名),password(密碼)truename(真實姓名)sex(性別),education(學歷)province(省份),city(城市)和favorite(愛好)屬性,並分別爲這些屬性創建getset方法,重寫ActionSupport類的execute()方法。

package com;

import com.opensymphony.xwork2.ActionSupport;

public class PersonFormAction extends ActionSupport {
	private String username;
	private String password;
	private String truename;
	private String sex;
	private String education;
	private String province;
	private String city;
	private String favorite;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getTruename() {
		return truename;
	}
	public void setTruename(String truename) {
		this.truename = truename;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getEducation() {
		return education;
	}
	public void setEducation(String education) {
		this.education = education;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getFavorite() {
		return favorite;
	}
	public void setFavorite(String favorite) {
		this.favorite = favorite;
	}
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}


2) 打開項目目錄的struts.xml文件,向該文件添加PersonFormAction配置。

<?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>
	<package name="com" extends="struts-default" namespace="/">
		<action name="dologin" class="com.PersonFormAction">
			<result>
				/personinfo.jsp
			</result>
		</action>
	</package>
</struts>    


3) 新建一個forme1.jsp頁面,在該頁面中使用<s:form>標籤,並使用表單元素標籤創建一些表單元素,以供用戶輸入或選擇相關的信息。具體內容如下:用<s:textfield>標籤完成用戶名的輸入,用<s:password>標籤完成密碼的輸入,用<s:textfield>標籤完成真實姓名的輸入,用<s:radio>完成性別的選擇,用<s:select>完成最高學歷的選擇(學歷包括高中、大學、碩士或博士),用<s:doubleselect>完成所在省份的城市的選擇,用<s:checkboxlist>完成個人愛好的選擇,用<s:submit>標籤提交。

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>填寫信息表單</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<s:form action="dologin" method="post">
		<s:textfield name="username" label="用戶名"></s:textfield>
		<s:password name="password" label="密碼"></s:password>
		<s:textfield name="truename" label="真實姓名"></s:textfield>
		<s:radio label="性別" list="{'男','女'}" name="sex"></s:radio>
		<s:select label="學歷" list="{'高中','大學','碩士','博士'}" name="education"></s:select>
		
		<s:set name="pc"
			value="#{'廣東省':{'廣州市','深圳市','東莞市'},'江蘇省':{'南京市','蘇州市','揚州市'},
		'河南省':{'鄭州市','洛陽市','開封市','南陽市'}}"></s:set>
		<s:doubleselect name="province" label="所在地" doubleList="#pc[top]" list="#pc.keySet()"
			doubleName="city"></s:doubleselect>
		
		<s:checkboxlist label="愛好" list="{'籃球','足球','棒球','游泳'}"
			name="favorite"></s:checkboxlist>
		
		<s:submit value="提交"></s:submit>
	</s:form>
</body>
</html>


4) 新建一個personinfo.jsp頁面,用於顯示用戶提交的個人信息。

 

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>表單內容</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	用戶名:<s:property value="username"/>
	<br> 密碼:<s:property value="password"/>
	<br> 真實姓名:<s:property value="truename"/>
	<br> 性別:<s:property value="sex"/>
	<br> 學歷:<s:property value="education"/>
	<br> 省份:<s:property value="province"/>
	<br> 城市:<s:property value="city"/>
	<br> 愛好:<s:property value="favorite"/>
	<br>

</body>
</html>

效果圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章