【Web开发】Struts2标签使用——非表单UI标签

Struts2标签库
1.actionerror:用于输出Action实例的getActionErrors()方法返回的错误提示信息
2.actionmessage:用于输出Action实例的getActionMessage()方法返回的错误提示信息
3.component:用于生成一个自定义组件
4.tree:用于生成一个树状结构
5.treenode:用于生成树状结构的节点,与tree标签一起使用

1.actionerror和actionmessage标签

MsgAction.java

package ch4;

import com.opensymphony.xwork2.ActionSupport;

public class MsgAction extends ActionSupport{
    public String execute(){
        addActionError("ActionError1");
        addActionError("ActionError2");
        addActionMessage("ActionMessage1");
        addActionMessage("ActionMessage2");
        return SUCCESS;
    }
}

struts.xml

<action name="msg" class="ch4.MsgAction">
    <result>/Jspch4/errorMessageTag.jsp</result>
</action>

errorMessageTag.jsp

<s:actionerror/>
<s:actionmessage/>

actionArror和actionMessage标签测试

2.component标签

theme:用于指定自定义组件所使用的主题,如果不指定该属性,则默认使用xhtml主题。
templateDir:用于指定自定义组件使用的主题目录,如不指定,则默认使用template目录。
template:用于指定自定义组件使用的模板文件。

<s:component template="mytemplate.jsp">
    <s:param name="list" value="{'a','b','c','d'}"/>
</s:component>

mytemplate.jsp 如未指定templateDir,则默认放在WebContent/template/xhtml文件夹下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<div style="background-color: #eeeeee;">
    <b>JSP自定义模板<br>列表:<s:select list="parameters.list"/></b>
</div>
</body>
</html>

component标签测试
3.tree标签和treenode标签

这个标签跟之前的datetimepicker一样变成struts-dojo-tags包里的了,所以跟之前一样,导入struts2-dojo-plugin-2.3.34.jar包,并在页面上加上:
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<sx:head parseContent="true"/>加在head里

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sx:head parseContent="true"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>使用tree和treenode生成静态树</h3>
<sx:tree label="计算机编程系列图书" id="books" 
    showRootGrid="true" showGrid="true" treeSelectedTopic="treeSelected">
    <sx:treenode label="Java基础" id="ajax">
        <sx:treenode label="Java程序设计基础" id="javascript"/>
        <sx:treenode label="Java实践教程" id="javascript"/>
    </sx:treenode>
    <sx:treenode label="Web框架" id="ajax">
        <sx:treenode label="JSF开发从入门到放弃" id="JSF"/>
        <sx:treenode label="Struts2开发从入门到放弃" id="Struts2"/>
    </sx:treenode>
</sx:tree>
</body>
</html>

tree和treenode标签测试

猜你喜欢

转载自blog.csdn.net/tjj1998/article/details/80555507