struts-4-标签
来源:互联网 发布:51单片机时钟电路图 编辑:程序博客网 时间:2024/05/16 06:12
环境:
TestAction类:
eclipse+jdk8+tomcat+struts-2.3.32-min-lib.zip
struts2的标签库体系结构:
标签库
普通标签 UI标签
Control标签 Data标签Form标签 Non-Form标签 Ajax标签
struts2的标签被定义在struts-tags.tld文件中,可以在struts-core.版本号.jar文件的META-INF目录
中找到它
在jsp页面中使用<%@taglib prefix="s" uri="/struts-tags"%>便可以使用struts标签
案例(查看操作效果,请看xml中的提示):
<!-- package 8 --><package name="sturtsTAGhtml" extends="struts-default"><action name="test_*" method="{1}" class="com.core.TestAction"><!-- 访问url:http://localhost:8080/strutsNote1/struts2tag/test.jsp点击“查看非表单标签”按钮 --><result name="test01">test.jsp</result><!-- 访问url:http://localhost:8080/strutsNote1/struts2tag/test_test02查看打印结果 --><result name="test02">test.jsp</result></action></package>
TestAction类:
package com.core;import java.lang.reflect.Field;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ServletRequestAware;import com.entity.User;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.util.ValueStack;public class TestAction extends ActionSupport implements ServletRequestAware{//获取request的方法一private HttpServletRequest request;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String test01(){String value="对应的action类需要extends ActionSupport";this.addActionError(value + "------this.addActionError");this.addActionMessage(value + "------this.addActionMessage");this.addFieldError("msg", value + "------this.addFieldError");return "test01";}public String test02(){//获取request的方法二 :传说可以解决中文乱码HttpServletRequest request = ServletActionContext.getRequest();//第一种方式获取ValueStackValueStack valueStack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);//静态变量: ServletActionContext.STRUTS_VALUESTACK_KEY = struts.valueStackSystem.out.println("======"+ServletActionContext.STRUTS_VALUESTACK_KEY);//第二种方式获取ValueStackValueStack valueStack2 = ActionContext.getContext().getValueStack();System.out.println("地址: "+valueStack+"\r\n地址:"+valueStack2); Field[] field = valueStack.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组 for(int j=0 ; j<field.length ; j++){ //遍历所有属性 String name = field[j].getName(); //获取属性的名字 System.out.println("属性名: "+name);}return "test02";}}
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML><html> <head> <title>My JSP 'login.jsp' starting page</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"> </head> <body> uri:<br> http://localhost:8080/strutsNote1/struts2tag/test.jsp<br><br> <h1>struts2的控制标签:</h1> <h3>1:</h3> <s:if test="true"> <pre> <s:if test="true"> 标签体 </s:if> <s:elseif test="false"> 标签体elseif </s:elseif> <s:else> 标签体else </s:else> </pre> </s:if> <s:elseif test="false"> 标签体elseif </s:elseif> <s:else> 标签体else </s:else> <h3>2:</h3> <pre> <s:iterator var="name" status="st" value="{'java','javaweb','oracle','mysql'}" begin="0" end="3" step="2"> value设置集合或数组 利用var取到单个值 begin开始的下标 end结束的下标 step每次迭代在原先下标基础上增加的值 status利用他的值获取迭代的状态 <s:iterator var="name" status="st" value="{'java','javaweb','oracle','mysql'}" begin="0" end="3" step="2"> <p> <p> <s:property value="name>当前下标的值: : <s:property value="name"/> <s:property value="#st.count"/>已经迭代的个数 : <s:property value="#st.count"/> <s:property value="#st.first"/>当前遍历的是否是第一个元素 : <s:property value="#st.first"/> <s:property value="#st.last"/>当前遍历的是否是最后一个元素 : <s:property value="#st.last"/> <s:property value="#st.index"/>当前遍历的索引值 : <s:property value="#st.index"/> <s:property value="#st.even"/>当前遍历索引是否为偶数 : <s:property value="#st.even"/> <s:property value="#st.odd"/>当前遍历索引是否为奇数 : <s:property value="#st.odd"/> </p> </p> </s:iterator> </s:iterator> </pre> <h1>数据标签</h1> <h3>1:</h3> <pre> 输出字符串 : <s:property value="'www.baidu.com'"/> <s:property value="'www.baidu.com'"/> 忽略html代码: <s:property value="<h3>are you good</h3>" escape="true"/> <s:property value="'<h3>are you good</h3>'" escape="true"/> 不忽略html代码: <s:property value="<h3>are you good</h3>" escape="false"/> <s:property value="'<h3>are you good</h3>'" escape="false"/> 输出默认值:<s:property value="" default="输出default属性的值"/> <s:property value="" default="输出default属性的值"/> </pre> <h3>2:</h3> <pre> <s:a> </s:a>对应html a标签 <s:debug/> 不知道有什么用处 <s:include value=""> </s:include> 将另一个jsp页面包括进来 <s:param> </s:param> 设置参数和s:include 一起使用 </pre> <h1>表单标签Struts2的元素好像有action的都有一个namespace设定命名空间</h1> <s:form> <s:textfield></s:textfield> <s:password></s:password> <s:radio name="sex" label="性别" value="0" list="#{'0':'男','1':'女' }"></s:radio> <s:checkbox label="用户" name="user" value="true" fieldValue="user"></s:checkbox> <s:checkbox label="管理员" name="manager_user" value="false" fieldValue="user"></s:checkbox> <s:checkbox label="老板" name="boss" value="false" fieldValue="user"></s:checkbox> <s:checkboxlist list="{'足球','篮球','乒乓球'}" name="interest" label="爱好"></s:checkboxlist> <s:file name="upLoadFile" ></s:file> <s:select label="选择内容" name="course" list="#{'javaweb','java','scala','hadoop' }" listKey="value" listValue="key"> <s:optgroup label="java" list="#{'jsp':'jsp','servlet':'servlet','javaBean':'javaBean'}" listKey="value" listValue="key"></s:optgroup> <s:optgroup label="javaweb" list="#{'spring':'spring','hibernate':'hibernate' }" listKey="value" listValue="key"></s:optgroup> </s:select> <s:reset value="重置" name="reset"></s:reset> <s:textarea></s:textarea> <s:submit value="提交"></s:submit> </s:form> <h1>非表单标签</h1> <a href="test_test01">查看非表单标签</a> <s:actionerror/> <s:actionmessage/> <s:fielderror></s:fielderror> <br/><br/><br/><br/><br/><br/> </body></html>
阅读全文
0 0
- struts标签4
- struts(4)-----html标签
- struts-4-标签
- struts标签
- Struts标签
- Struts标签
- struts标签
- struts 标签
- Struts标签
- Struts标签
- struts标签
- Struts标签
- Struts标签
- struts标签
- struts标签
- struts标签
- Struts标签
- Struts标签
- JAVA面试问题—基础篇(上)
- ThinkPHP Model的方法
- Visual Studio Web Deploy发布网站
- 虚拟机安装了xp,互相ping不通
- Kotlin-Java中调用Kotlin
- struts-4-标签
- ARM架构和X86架构区别
- POJ 3189 Steady Cow Assignment 笔记
- 证明NP问题(算法概论8.8)
- SecureCRT安装使用
- BroadcastReceiver实现唤起已关闭应用的功能
- (Java代码实现)单链表有环的一系列问题
- Java培训第02天 Java基础知识(一)---2017年07月11日
- LIGHT OJ 1189