概述
Struts2的UI标签简单并且易于使用,你不用写任何html代码,UI标签会根据你的选择的主题自动的生产它们,默认情况下使用XHTML主题,XHTML主题使用表格来定位表单元素。
那么UI标签是怎样工作的,在Struts2中,在Struts2中ValueStack(值栈)存放着请求处理相关的数据,因此,所有表单的内容都将被存储在ValueStack里,UI标签的属性名对应这值栈里面的值。。
其实标签这东西只要了解一些格式和一些常用属性就可以了,用多了就熟悉了。
下面使用UI标签创建一个注册页面:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Register Page</title>
</head>
<body>
<h2>This is my second struts application</h2>
<s:form action="Register" method="post">
<s:textfield name="userName" label="User Name" />
<s:password name="password" label="Password" />
<s:radio name="gender" label="Gender" list="{'Male','Female'}" />
<s:select name="country" list="countryList" listKey="countryId"
listValue="countryName" headerKey="0" headerValue="Country"
label="Select a country" />
<s:textarea name="about" label="About You" />
<s:checkboxlist list="communityList" name="community" label="Community" />
<s:checkbox name="mailingList" label="Would you like to join our mailing list?" />
<s:submit />
</s:form>
</body>
</html>
Struts.xml配置文件如下:
<package name="register" extends="struts-default">
<action name="*Register" class="com.zcl.struts.register.RegisterAction" method="{1}">
<result name="populate">/register.jsp</result>
<result name="input">/register.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
在首页我们加上如下代码:
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateRegister.action">
在这里,主要是请求跳转到populateRegister。根据struts.xml文件可知,RegisterAction 类中的populate方法将被调用,在register Action类里包含了表单的内容和相关的setter和getter方法,也包含了populate方法和execute方法,在populate方法里我们首先填充值然后在设置表单字段的默认人值,RegisterAction代码如下:
package com.zcl.struts.register;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String userName ;
private String password ;
private String gender ;
private String about ;
private String country ;
private ArrayList<Country> countryList ;
private String[] community ;
private ArrayList<String> communityList ;
private boolean mailingList ;
public String populate(){
countryList = new ArrayList<Country>() ;
countryList.add(new Country(1,"China")) ;
countryList.add(new Country(2,"USA")) ;
countryList.add(new Country(3,"English")) ;
communityList = new ArrayList<String>() ;
communityList.add("JAVA") ;
communityList.add("C++") ;
communityList.add("PHP") ;
community = new String[]{"Java","C++"} ;
mailingList = true ;
return "populate" ;
}
public String execute(){
return SUCCESS ;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public ArrayList<Country> getCountryList() {
return countryList;
}
public void setCountryList(ArrayList<Country> countryList) {
this.countryList = countryList;
}
public String[] getCommunity() {
return community;
}
public void setCommunity(String[] community) {
this.community = community;
}
public ArrayList<String> getCommunityList() {
return communityList;
}
public void setCommunityList(ArrayList<String> communityList) {
this.communityList = communityList;
}
public boolean isMailingList() {
return mailingList;
}
public void setMailingList(boolean mailingList) {
this.mailingList = mailingList;
}
}
上面用到了Country类代码如下:
package com.zcl.struts.register;
public class Country {
private int countryId ;
private String countryName ;
public Country(){}
public Country(int countryId, String countryName) {
this.countryId = countryId;
this.countryName = countryName;
}
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
下面是成功接收注册信息suucess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>success page</title>
</head>
<body>
<h2>Register success</h2>
User Name: <s:property value="userName"/><br/>
Gender
: <s:property value="gender"/><br/>
Country
: <s:property value="country"/><br/>
About you: <s:property value="about"/><br/>
Community: <s:property value="community"/><br/>
MailingList: <s:property value="mailingList"/>
</body>
</html>
运行效果如下:
成功页面:
最后
以上就是温柔冬天为你收集整理的一些简单的UI标签的全部内容,希望文章能够帮你解决一些简单的UI标签所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复