Struts学习(一)Struts标签库的运用

  

什么是标签库?

  JSP标签库,也称自定义标签库,可看成是一种通过JavaBean生成基于XML的脚本的方法。从概念上讲,标签就是很简单而且可重用的代码结构。比方说,在最新发布的JSPKit(在JSP Insider内)中,使用XML标签实现了对XML文档的轻松访问。

  简单的说类似与前端的jar,便于开发前端,直接使用已开发的标签。

  常见的标签库有:JSTL等

struts1标签库与其他标签库的区别?

  struts1是基于struts框架所开发的,脱离了struts框架,无法使用,而其他标签库没有这一类的限制

Struts1主要元素和常用标签

  struts1主要存在<bean>、<logic>、<html>标签库

  本文主要介绍常用的标签,由于脱离struts框架无法使用其标签库,所以该标签库日常工作使用不多

  <bean:write>:输出

  <logic:empty> 和 <logic:notEmpty>:判断变量是否为空

  <logic:present> 和<logic:notPresent>:判断变量是否存在

  <logic:iterator>:迭代

<bean:write>标签

  用途:主要用于对于Request中的数据进行jsp页面的输出包括(原始输出和格式化输出)

  (ps我们复习一下简易Struts的配置

  action路径==》struts-config.xml中:path:配置标识,用于这样的URL会进入该Action(最前面加“/”)

                        type:配置action的全限定类名

  servlet路径==》web.xml中,由于struts中servlet有jar包中来,配置jar中路径,

         其中servlet_mapping配置URL尾缀规则)

  jsp使用href标签访问URL时,该URL应为path+url_pattern(不带“/”) 

  导入bean标签库:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

  普通输出:

//普通字符串
        request.setAttribute("hello","helloworld");
    hello(标签):<bean:write name="hello"/><br>

  filter元素的使用,HTML文本输出,默认filter=false,如需解析HTML,则将其设为true

 //html文本
        request.setAttribute("bj","<font color = 'red'>北京欢迎你</font>");
    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"/><br>

  format元素的使用,用于格式化输出,通常使用于日期和数字上

  

        //日期
        request.setAttribute("today",new Date());

        //数字
        request.setAttribute("n",12345.1234);

其中###,###.#####小数点后不会自动补0,而###,###.00000小数点后会自动补0

<li>测试格式化日期</li>
    today(default):<bean:write name="today"/><br>
    today(format="yyyy-mm-dd hh:mm:ss"):<bean:write name="today" format="yyyy-mm-dd hh:mm:ss"/><br>
    <p>
    <li>测试格式化数字</li>
    n(default):<bean:write name="n"/><br>
    n(format="###,###.#####"):<bean:write name="n" format="###,###.#####"/><br>
    n(format="###,###.00000"):<bean:write name="n" format="###,###.00000"/><br>

   结构元素的输出

//结构对象
        Group group = new Group();
        group.setName("动力节点");

        User user = new User();
        user.setUsername("yyyaaaooo");
        user.setAge(22);
        user.setGroup(group);

        request.setAttribute("user",user);
<li>测试结构</li><br>
    姓名:<input type="text" name = "username" value="<bean:write name="user" property="username"/>">
    年龄:<input type="text" name = "age" value="<bean:write name="user" property="age"/>">
    所属组:<input type="text" name = "age" value="<bean:write name="user" property="group.name"/>">

值得注意的是:

  1.property元素中,<bean:write>的输出,在服务层已经完成,“”对其不产生影响

  2.所有的结构元素都需要有get和set方法,框架中自动会调用其get方法

  页面样例:

 

 <logic:empty> <logic:notEmpty> <logic:present> <logic:notPresent>标签

  用途:做一些判空、判是否存在的逻辑校验

  导入logic标签库: 

<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>

  使用标签:

    <logic:empty name="attr1">
        attr1为空<br>
    </logic:empty>
    <logic:notEmpty name="attr1">
        attr1不为空<br>
    </logic:notEmpty>
    <logic:present name="attr1">
        attr1存在<br>
    </logic:present>
    <logic:notPresent name="attr1">
        attr1不存在<br>
    </logic:notPresent>
request.setAttribute("attr1",null);
request.setAttribute("attr2","");
request.setAttribute("attr3",new ArrayList());

  页面样例:

  <logic:iterate> 标签

  用途:用于遍历获取输出值

  其中,将本章所学习的标签都复习了一遍

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List userList = new ArrayList<>();
        Group group = new Group();
        group.setName("动力节点");
        for(int i=0;i<10;i++){
            User user = new User();
            user.setUsername("yyyaaaooo");
            user.setAge(22+i);
            user.setGroup(group);

            userList.add(user);
        }

        request.setAttribute("user_List",userList);

        return mapping.findForward("success");
    }
<table border="1">
        <tr>
            <td>姓名</td>
            <td>性别</td>
            <td>所属组</td>
        </tr>
        <logic:empty name="user_List">
            <tr>
                <td colspan="3">没有符合条件的数据</td>
            </tr>
        </logic:empty>
        <logic:notEmpty name="user_List">
            <logic:iterate id="user" name="user_List">
                <tr>
                    <td>
                        <bean:write name="user" property="username"/>
                    </td>
                    <td>
                        <bean:write name="user" property="age"/>
                    </td>
                    <td>
                        <bean:write name="user" property="group.name"/>
                    </td>
                </tr>
            </logic:iterate>
        </logic:notEmpty>
    </table>

<logic:iterate>标签中:id为list中单个存放的元素,name为list本身

介于小编前端能力不足,补充整理前端标签知识:

  <h1>标题标签,能够清晰用户知道网络的重要部分

  <hr>划一条水平线

  <p>与<br>均为换行

    <p>为换一大行 ,下一行空缺,再下一行开始  

    <br>为下一行开始  

  <input> type元素代表输入类型,name为该行元素名字,value为输入框内的值;

  ej:

   姓名:<input type="text" name = "username" />

   

  列表显示:

--有序列表
<ol> <li>coffee</li> <li>tea</li> <li>milk</li> </ol>
--无序列表
<ul> <li>coffee</li> <li>tea</li> <li>milk</li> </ul>

  li表示点的显示

  表显示:

  

<table border="1">
    <tr>
      <td>姓名</td>
      <td>性别</td>
      <td>所属组</td>
  </tr>
  <tr>
      <td>yaoyao</td>
      <td></td>
      <td>1</td>
  </tr>
</table>

  

猜你喜欢

转载自www.cnblogs.com/yyfighting2019/p/11873384.html