Struts2 framework study notes 7: struts2 tag, struts2 Study Notes
Three labels:
1. JSP: script. Expired to replace servlet
2. JSTL: Standard tag Library (core, format, SQL, xml). Only core libraries are eliminated.
3. Struts2 tag Library: Developed by the Struts2 Development Team, with more powerful functions
Struts2 labels include common labels and UI labels.
Common Tag: Control tag, data tag
UI Tag: Form tag, non-form tag
Prepare some data:
List<String> list = new ArrayList<>(); list.add("tom"); list.add("jerry"); list.add("jack"); list.add("rose"); ActionContext.getContext().put("list", list);
Common tag example:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Form tags (not commonly used ):
<! -- Struts2 form tag --> <! -- Benefit 1: a set of built-in styles. --> <! -- Benefit 2: Automatic Echo, based on the attributes in the stack --> <! -- Theme: specify the form subject xhtml: default simple: No topic --> <s: form action = "Demo3Action" namespace = "/" theme = "xhtml"> <s: textfield name = "name" label = "username"> </s: textfield> <s: password name = "password" label = "password"> </s: password> <s: radio list = "{'male', 'female '}" name = "gender" label = "gender"> </s: radio> <s: radio list = "# {1: 'male', 0: 'female '}" name = "gender" label = "gender"> </s: radio> <s: checkboxlist list = "# {2: 'Smoking ', 1: 'drinking', 0: 'playing game'}" name = "habits" label = "hobbies"> </s: checkboxlist> <s: select list = "# {2: 'Junior College ', 1: 'undergraduate', 0: 'master'} "headerKey =" "headerValue =" --- Please select --- "name =" edu "label =" "> </s: select> <s: file name = "photo" label = "Recent"> </s: file> <s: textarea name = "desc" label = "Personal Profile"> </s: textarea> <s: submit value = "submit"> </s: submit> </s: form> <s: actionerror/>
Action:
Package tag; import com. opensymphony. xwork2.ActionSupport; public class Demo3Action extends ActionSupport {private String name; public String execute () throws Exception {System. out. println (name); this. addActionError ("error"); return SUCCESS;} public String getName () {return name;} public void setName (String name) {this. name = name ;}}