struts2标签

阅读更多
1. struts2标签
   
Struts2自己封装了一套标签,比JSTL强大,而且与Struts2中的其他功能无缝结合。
当然Strust2标签的内容很多,随着版本的升级,标签和属性越来越多。我们要掌握好核心标签及了解其他标签;
根据功能可以分为:数据标签,控制标签,界面标签,其他标签;


新建项目HeadFirstStruts2Chap05

MyComparator.java

package com.andrew.comparator;
import java.util.Comparator;
import com.andrew.model.Student;
public class MyComparator implements Comparator {
    public int compare(Student s1, Student s2) {
        if (s1.getAge() > s2.getAge()) {
            return 1;
        } else if (s1.getAge() < s2.getAge()) {
            return -1;
        }
        return 0;
    }
}

Student.java

package com.andrew.model;
public class Student {
    private int id;
    private String name;
    private int age;
    public Student() {}
    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

struts.xml



jsp

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


2. Struts2数据标签

1) property标签:输出OGNL表达式的值

<% request.setAttribute("name","张三"); %>



http://localhost:8080/HeadFirstStruts2Chap05/data/property.jsp 张三 某某人 张三


2) set标签:设置变量








http://localhost:8080/HeadFirstStruts2Chap05/data/set.jsp 1 action范围的值 page范围的值 request范围的值 session范围的值 application范围的值


3) bean标签:定义javaBean对象


    
    
 



http://localhost:8080/HeadFirstStruts2Chap05/data/bean.jsp
张三 10


4) date标签:日期标签

<%    request.setAttribute("date",new Date()); %>
${date }
当前日期: http://localhost:8080/HeadFirstStruts2Chap05/data/date.jsp Sat Jan 05 15:44:04 CST 2018 当前日期:2018-01-05


5) debug标签:调试标签




6) url&a标签:超链接标签


    

超链接

    
    超链接2


http://localhost:8080/HeadFirstStruts2Chap05/foreground/hello.action?name=struts2
http://localhost:8080/HeadFirstStruts2Chap05/foreground/hello.action?name=struts2


7) include标签:动态包含标签



head.html

头部

http://localhost:8080/HeadFirstStruts2Chap05/data/include.jsp
头部


3.  Struts2控制标签

1) ifelse标签:条件判断标签

<%    int age=11;
    request.setAttribute("age",age); %>
年龄小于20岁
年龄等于20岁
年龄大于20岁

年龄小于20岁 


2) iterator标签:遍历标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List studentList=new ArrayList();
    studentList.add(new Student(1,"张三",10));
    studentList.add(new Student(3,"李四",20));
    studentList.add(new Student(5,"王五",30));
    request.setAttribute("studentList",studentList);
%>

    
序号编号姓名年龄
http://localhost:8080/HeadFirstStruts2Chap05/control/iterator.jsp 序号 编号 姓名 年龄 1 1 张三 10 2 3 李四 20 3 5 王五 30


3) append标签:叠加标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List studentList1=new ArrayList();
    List studentList2=new ArrayList();
    studentList1.add(new Student(1,"张三",10));
    studentList1.add(new Student(3,"李四",20));
    studentList2.add(new Student(5,"王五",30));
    studentList2.add(new Student(7,"赵六",40));
    request.setAttribute("studentList1",studentList1);
    request.setAttribute("studentList2",studentList2);
%>

    
    


    
序号编号姓名年龄
http://localhost:8080/HeadFirstStruts2Chap05/control/append.jsp 序号 编号 姓名 年龄 1 1 张三 10 2 3 李四 20 3 5 王五 30 4 7 赵六 40


4) generator标签:分隔标签



    


http://localhost:8080/HeadFirstStruts2Chap05/control/generator.jsp
张三 李四 王五


5) merge标签:组合标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List studentList1=new ArrayList();
    List studentList2=new ArrayList();
    studentList1.add(new Student(1,"张三",10));
    studentList1.add(new Student(3,"李四",20));
    studentList2.add(new Student(5,"王五",30));
    studentList2.add(new Student(7,"赵六",40));
    request.setAttribute("studentList1",studentList1);
    request.setAttribute("studentList2",studentList2);
%>

    
    


    
序号编号姓名年龄
http://localhost:8080/HeadFirstStruts2Chap05/control/merge.jsp 序号 编号 姓名 年龄 1 1 张三 10 2 5 王五 30 3 3 李四 20 4 7 赵六 40


6) sort标签:排序标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List studentList1=new ArrayList();
    studentList1.add(new Student(1,"张三",20));
    studentList1.add(new Student(3,"李四",10));
    studentList1.add(new Student(5,"王五",40));
    studentList1.add(new Student(7,"赵六",30));
    request.setAttribute("studentList1",studentList1);
%>


    
    
序号编号姓名年龄
http://localhost:8080/HeadFirstStruts2Chap05/control/sort.jsp 序号 编号 姓名 年龄 1 3 李四 10 2 1 张三 20 3 7 赵六 30 4 5 王五 40


7) subset标签:截取标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List studentList1=new ArrayList();
    studentList1.add(new Student(1,"张三",20));
    studentList1.add(new Student(3,"李四",10));
    studentList1.add(new Student(5,"王五",40));
    studentList1.add(new Student(7,"赵六",30));
    request.setAttribute("studentList1",studentList1);
%>

    
    
序号编号姓名年龄
http://localhost:8080/HeadFirstStruts2Chap05/control/subset.jsp 序号 编号 姓名 年龄 1 5 王五 40 2 7 赵六 30


4. Strut2界面标签

1) form标签:表单提交标签



http://localhost:8080/HeadFirstStruts2Chap05/ui/form.jsp


2) text标签:文本标签

用户名:
密码:
备注:
http://localhost:8080/HeadFirstStruts2Chap05/ui/text.jsp


3) radios标签:单选标签

性别:

http://localhost:8080/HeadFirstStruts2Chap05/ui/radio.jsp


4) checkboxlist标签:复选框标签

爱好:

http://localhost:8080/HeadFirstStruts2Chap05/ui/checkbox.jsp


5) select标签:下拉框标签

爱好:

http://localhost:8080/HeadFirstStruts2Chap05/ui/select.jsp



5. 其他标签

1) updownselect标签

 

http://localhost:8080/HeadFirstStruts2Chap05/ui/updownselect.jsp


2) optiontransferselect标签


   

http://localhost:8080/HeadFirstStruts2Chap05/ui/optiontransferselect.jsp

你可能感兴趣的:(Java,struts2)