Saturday, February 20, 2016

Struts Tags

In java ee standards, there are three types of tags
  1. customized tags
  2. jstl tags
  3. struts tags
*.tld files: tag lib description
for example struts-html.tld, in struts-taglib.jar, defines all html tags

<%@ taglib prefix="html" uri="" %>

<html:image> is a image button
<html:img> is just a image

paramName ="<name-of-the-form-bean-whose-property-you-want-to-use>" 
paramId ="<name-of-the-parameter>" .... in your case it will be paramId="action"

<logic:notpresent> vs <logic;empty>

usually notpresent is used for availability of roles so you can tailor your jsp pages based on roles. But it basically checks for the existance of objects. 

empty is for checking if strings (length of 0) or collections (null) are empty.

<html:form> must map to a ActionForm class!
And each property inside the form must map to a variable in the ActionForm
<html:form action="/">: action url must NOT include the webapp directory, and this action will automatically validate by struts BEFORE submit the form! If the action url is not reachable, this page will throw error

Bean Tags
For create, visit bean and bean properties.
Can create bean based on cookies, headers, and parameters
<bean:write> <!-- for output info -->
request.setAttribute("abc","hello world");
<bean:write name="abc"/>

<!-- for output value from object, the property other than String must have format configured
Otherwise, it will cause error "Cannot find message resources under key org.apache.struts.action.MESSAGE"
and Error "org.apache.jasper.servlet.JspServletWrapper.handleJspException"
Cat myCat = new Cat();
request.setAttribute("myCat", myCat);

<bean:write name="myCat" property="name"/>
<bean:write name="myCat" property="age" format="0"/>

Normally used for error message
To use <bean:message>, we need to set struts-config.xml with message-resources

  1. <message-resources parameter="com.gvace.web.struts.ApplicationResources"></message-resources>
  2. create file: com.gvace.web.struts/
  3. file content example: key1 = welcome {0}
  4. use the message: <bean:message key="key1" arg0="Yushan"/>
  5. error message example:
    err1 = your error is: {0}{1}
    <bean:message key="err1" arg0="Username is empty" arg1="Password is empty"/>
  6. For character encoding in properties file:
    use native2ascii from jdk/bin/, copy the String into native2ascii, it will translate to ascii
<bean:define> (not often used)

<!-- define a bean or its property -->

<bean:define id="kkk" name="abc" value="hello" scope="request"></bean:define>

<!-- jsp way to define a bean or its property -->
<jsp:useBean id="cat2" class="com.gvace.model.Cat"></jsp:useBean>
<jsp:setProperty name="cat2" property="name" value="tom"/>
<jsp:setProperty name="cat2" property="age" value="3"/>
${} ${cat.age}
<!-- the above tag equals the following code -->
Cat cat2 = new Cat();
not introduced

Logic Tags

<logic:iterate>: iterate through collection/array
List<Cat> al = new ArrayList<Cat>();
pageContext.setAttribute("cats", al);

<logic:iterate id="myCat" name="cats">
${}, ${myCat.age}<br />

<logic:empty> <logic:notEmpty>
<logic:empty name="cat1">cat1 not exist</logic:empty>
<logic:notEmpty name="cat1">cat1 is exist</logic:notEmpty>

<logic:greaterThan name="myCat" value="3" property="age">
cat: <bean:write name="myCat" property="name"/> age bigger than 20

Nested Tags(optional)

Tiles Tags(optional)

No comments:

Post a Comment