转自处:[url]http://www.vaannila.com/struts-2/struts-2-example/struts-2-ui-tags-example-1.html[/url]
搭建好struts2环境
web.xml配置
RegisterAction.java类
Country.java类
struts.xml配置
jsp文件
index.jsp
register.jsp
success.jsp
发布到tomcat后在浏览器中输入
[url]http://localhost:8080/Struts2_UITagsExample/[/url]
或
[url]http://localhost:8080/Struts2_UITagsExample/populateRegister.action[/url]
即可
附件是完整工程
搭建好struts2环境
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2_UITagsExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
RegisterAction.java类
package com.test;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport
{
private String userName;
private String password;
private String gender;
private String about;
private String country;
private List<Country> countryList;
private String[] community;
private List<String> communityList;
private Boolean mailingList;
public String populate()
{
countryList = new ArrayList<Country>();
countryList.add(new Country(1, "India"));
countryList.add(new Country(2, "USA"));
countryList.add(new Country(3, "France"));
communityList = new ArrayList<String>();
communityList.add("Java");
communityList.add(".Net");
communityList.add("SOA");
community = new String[] { "Java", ".Net" };
mailingList = true;
return "input";
}
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 List<Country> getCountryList()
{
return countryList;
}
public void setCountryList(List<Country> countryList)
{
this.countryList = countryList;
}
public String[] getCommunity()
{
return community;
}
public void setCommunity(String[] community)
{
this.community = community;
}
public List<String> getCommunityList()
{
return communityList;
}
public void setCommunityList(List<String> communityList)
{
this.communityList = communityList;
}
public Boolean getMailingList()
{
return mailingList;
}
public void setMailingList(Boolean mailingList)
{
this.mailingList = mailingList;
}
}
Country.java类
package com.test;
public class Country
{
private int countryId;
private String countryName;
public Country(int id,String name)
{
countryId = id;
countryName = name;
}
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;
}
}
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>
<include file="struts-default.xml"/>
<package name="com.test" extends="struts-default">
<action name="*Register" method="{1}" class="com.test.RegisterAction">
<result name="populate">/register.jsp</result>
<result name="input">/register.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
jsp文件
index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateRegister.action">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register Page</title>
</head>
<body>
<s:form action="Register">
<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>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Details Page</title>
</head>
<body>
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>
Mailing List:
<s:property value="mailingList" />
</body>
</html>
发布到tomcat后在浏览器中输入
[url]http://localhost:8080/Struts2_UITagsExample/[/url]
或
[url]http://localhost:8080/Struts2_UITagsExample/populateRegister.action[/url]
即可
附件是完整工程