zoukankan      html  css  js  c++  java
  • struts2标签(五)

    标签体系结构

      jsp出现目的是为了取代servlet,结果逻辑代码,数据库代码都放到了jsp页面中。

      为了解决jsp中代码过多的问题,struts2标签分为普通标签和UI标签。

    使用struts2标签之前首先要引入标签库 

      <%@ taglib prefix="s" uri="/struts-tags" %>

    普通标签

      控制标签

        iterator(迭代)

        public String execute(){
    
            List<String> list = new ArrayList<String>();
            list.add("tom");
            list.add("jack");
            list.add("rose");
    
            ActionContext.getContext().put("list",list);
            return SUCCESS;
        }
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!--引入标签 -->
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
      <body>
      <s:debug></s:debug>
      <s:iterator value="list">
        <s:property></s:property>
      </s:iterator>
    
      <s:iterator begin="1" end="100" step="1">
        <s:property></s:property>
      </s:iterator>
      </body>
    </html>

        if elseif else(判断)

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!--引入标签 -->
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <%
        request.setAttribute("score", 100);
    %>
    <body>
        <s:if test="#request.score>90"></s:if>
        <s:elseif test="#request.score>60">
            及格
        </s:elseif>
        <s:else>
            不行
        </s:else>
    </body>
    </html>

      数据标签

        property 取出值栈中的数据

    <s:property value="#request.score"/>

    UI标签

      表单标签

        form

        textfield

        password

        checkboxlist

        radio

    <s:form action="register" namespace="">
        <s:textfield name="username" label="用户名" key="Switch"/>
        <s:password name="password" label="密码"/>
        <s:radio name="gender" list="#{'0':'男','1':'女'}" label="性别" value="0" />
        <s:textfield name="age" label="年龄"/>
        <s:select name="city" list="#{'bj':'北京','sh':'上海','gz':'广州','sz':'深圳'}" label="城市" headerKey="-1" headerValue="---请选择城市---" emptyOption="true"/>
        <s:checkboxlist name="hibbies" list="#{'code':'写代码','algorithm':'算法','movie':'电影'}" label="爱好"/>
        <s:checkbox name="married" label="是否已婚" value="true" labelposition="left"/>
        <s:textarea name="description" label="自我介绍" rows="5" cols="20"/>
        <s:file name="phone" label="头像"/>
        <s:submit value="提交"/>
        <s:reset value="重置"/>
    </s:form>

      非表单标签

        actionerror 后台设置信息 前台展示

    this.addActionError("呵呵 错误信息");
    <s:actionerror></s:actionerror>
  • 相关阅读:
    第二冲刺阶段个人博客
    单元测试
    软件工程个人总结
    梦断代码阅读笔记03
    梦断编码阅读笔记02
    梦断代码阅读笔记01
    padding margin
    MVC模式在Java Web应用程序中的实例分析
    小问题
    MVC理解
  • 原文地址:https://www.cnblogs.com/baidawei/p/9072461.html
Copyright © 2011-2022 走看看