菜鸟学习Struts——bean标签库

最后更新于:2022-04-01 09:33:32

## 一、Struts标签库。 Struts实际上包含了4个标签库:bean,logic,html,tiles bean:用来在属性范围中定义或取得属性的,同时可以读取资源文件信息 logic:替代JSTL中的判断和循环 html:替代页面表单,完成自动回填以及错误信息的显示 tiles:美工标签,用来进行页面布局的调整以及样式管理的。 ## 二、各个标签库的实例 下面利用实例来为大家降解三个标签库: beanwrite标签库、logic标签库、iterate标签库 ### 2.1 beanwrite标签 实现效果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-22_56caddf9ece18.jpg) 代码: #### 2.1.1、[配置Struts环境](http://blog.csdn.net/gwblue/article/details/18367901) #### 2.1.2、编写bean_write.jsp ~~~ <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <!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=GB18030"> <title>Insert title here</title> </head> <body> <h1>测试beanWrite标签</h1><br> <hr> <li>普通字符串</li><br> hello(标签):<bean:write name="hello"/> <p> <li>html文本</li><br> bj(default):<bean:write name="bj"/> <br> bj(filter:"true"):<bean:write name="bj" filter="true"/> <br> bj(filter:"false"):<bean:write name="bj" filter="false"/> <p> <li>user</li><br> 姓名:<input type="text" name="username" value="<bean:write name='user' property='username'/>"><br> 年龄:<input type="text" name="age" value="<bean:write name='user' property='age'/>"><br> 所属组:<input type="text" name="group" value="<bean:write name='user' property='group.name'/>"><br> </body> </html> ~~~ #### 2.1.3、编写BeanWriteTestAction.java ~~~ package com.bjpowernode.struts; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 测试beanWrite标签 * @author IT * */ public class BeanWriteTestAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //普通字符串 request.setAttribute("hello", "helloword"); //html文本 request.setAttribute("bj", "<font color='red'>北京欢迎你</font>"); //日期 request.setAttribute("today",new Date()); Group group=new Group(); group.setName("动力节点"); User user=new User(); user.setUsername("张三"); user.setAge(22); user.setGroup(group); request .setAttribute("user", user); return mapping.findForward("success"); } } ~~~ #### 2.1.4、编写实体类 User.java ~~~ package com.bjpowernode.struts; /** * User实体 * @author IT * */ public class User { //用户名字 private String username; //年龄 private int age; //组 private Group group; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } } ~~~ Group.java ~~~ package com.bjpowernode.struts; /** * 组的实体类 * @author IT * */ public class Group { //组名 private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } ~~~ #### 2.1.5、struts-config.xml配置 ~~~ <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings> <action path="/beanwrite" type="com.bjpowernode.struts.BeanWriteTestAction" > <forward name="success" path="/bean_write.jsp"></forward> </action> </action-mappings> </struts-config> ~~~
';