`
plane
  • 浏览: 160737 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Struts2 UI Tags Example

    博客分类:
  • java
阅读更多
转自处:http://www.vaannila.com/struts-2/struts-2-example/struts-2-ui-tags-example-1.html

搭建好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后在浏览器中输入
http://localhost:8080/Struts2_UITagsExample/

http://localhost:8080/Struts2_UITagsExample/populateRegister.action
即可

附件是完整工程
分享到:
评论

相关推荐

    struts2简单例子

    Struts2支持应用的多语言支持和UI主题定制,使得应用更易国际化和美化。 8. ** strut2-tags** 文件夹: 可能包含了演示Struts2 JSP标签的示例代码,帮助开发者理解如何在实际项目中使用这些标签。 通过学习和...

    Struts2 自定义标签

    import org.apache.struts2.views.jsp.ui.TagSupport; public class CustomTag extends TagSupport { private String attribute1; private String attribute2; // getter & setter @Override public int ...

    struts2标签介绍

    下面我们将从Struts1.x与Struts2.0标签库的对比入手,再深入探讨Struts2中的非UI标签。 ##### 2.1 Struts1.x与Struts2.0 标签库对比 - **分类**:Struts1.x的标签库按照功能划分为HTML、Tiles、Logic和Bean等几个...

    自定义struts2标签

    1. **定义标签接口**:Struts2标签库基于OGNL(Object-Graph Navigation Language)表达式,因此我们需要继承`org.apache.struts2.views.jsp.TagSupport`或`org.apache.struts2.views.jsp.ui.Tag`类来创建自定义标签...

    struts2的自定义标签

    2. **创建标签处理类(Tag Handler Class)**:标签处理类是实现自定义标签逻辑的核心,它需要继承自Struts2提供的基类,如`org.apache.struts2.views.jsp.ui.TagSupport`。在该类中,我们需要重写父类的方法来处理...

    struts2标签详解

    &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; ``` 这行代码指定了 Struts2 标签库的前缀为 "s",并且指定了 Struts2 标签库的 URI。 #### 三、条件判断标签 ##### 3.1 `&lt;s:if&gt;` 标签 - **描述**:用于执行基本...

    Struts2中实现自定义分页标签 --功能扩充

    在`src`目录下,创建一个新的包,例如`com.example.struts2.customtags`,用于存放自定义标签的相关类。我们需要一个继承自`org.apache.struts2.views.jsp.ui.TagSupport`的类,如`PagingTag`,用于处理分页逻辑。 ...

    Strut2 in action [英文版]

    - **定义**:Struts2还提供了一系列UI组件标签,如下拉列表、复选框等,这些标签可以帮助开发者快速构建复杂的用户界面。 - **示例代码**: ```jsp &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; ,'USA','...

    Struts 中标准的MVC

    Struts提供了丰富的标签库(Tiles、Struts Tags等),使得开发者可以方便地在JSP页面中构建UI,同时通过ActionForm对象收集用户输入,实现数据验证,确保数据安全。例如,`&lt;s:textfield&gt;`标签用于创建表单字段,`...

    下拉框自定义标签

    1. **定义标签接口**:通常我们会继承`org.apache.struts2.views.jsp.ui.TagSupport`,它是Struts2提供的基础标签类,包含了处理标签的基本方法。 2. **实现标签逻辑**:在自定义标签类中,我们需要实现获取下拉框...

Global site tag (gtag.js) - Google Analytics