Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

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

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

<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"
paramProperty="<value-of-the-parameter-which-is-actually-the-value-of-the-property-of-the-form-bean-uhave-specified-above>"



<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="/login1.do">: 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();
myCat.setName("ccc");
myCat.setAge(3);
request.setAttribute("myCat", myCat);
%>

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

<bean:message>
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/ApplicationResources.properties
  3. ApplicationResources.properties 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.name} ${cat.age}
<!-- the above tag equals the following code -->
<%
Cat cat2 = new Cat();
cat2.setName("tom");
cat2.setAge(3);
%>
<bean:page>
not introduced

Logic Tags

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

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

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

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



Nested Tags(optional)



Tiles Tags(optional)


Wednesday, February 17, 2016

JSTL

jstl(jsp standard tag library)

Version:
Not lower than JSP1.2 and Servlet 2.3 can use jstl
JSP2.0 can use jstl1.2
  1. normal usage tag
  2. conditional tag
  3. iterator tag
  4. url related tag(xml, sql)
Config jstl library
We need two librarys: jstl and taglibs-standard



import

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

Four levels, 



  1. pageContext.setAttribute("abc", "abc1
  2. <c:if test="${empty abc}">
  3. abc is empty
  4. </c:if>
  5. <c:if test="${!empty abc}">
  6. abc is not empty
  7. </c:if>
  8. ");
  9. request.setAttribute("abc", "abc2");
  10. session.setAttribute("abc", "abc3");
  11. application.setAttribute("abc", "abc3");

${pageContext.request.contextPath} is the contextPath I prefer to use, it returns the web app path, as same as request.getContextPath()

empty
detect if value is null, "", empty array, empty map or empty collection
<c:if test="${empty abc}">
abc is empty
</c:if>
<c:if test="${!empty abc}">
abc is not empty

</c:if>

quick condition
${2>1?"bigger":"smaller"}

func(arg,arg1,arg2)
call a function with zero to multiple arguments

<c:out>
<c:out value="${abc}"></c:out>

${} is the mark for that enclosed value is a variable or expression
With the same attribute name, c:out value always looking for the lowest level value first.

<c:out value="${abc}" default="default_value"></c:out> : default value if no attribute found in any level

<c:out value="${abc}"  escapeXml="false"></c:out>
true: make the output to be text only
false: make the output to be html
default is true


<c:out value="${user.name}"></c:out>: user is an object, name is the field of the object
Which equals ((User)request.getAttribute("user")).getName();

<c:set>
<c:set var="abc" value="abc4" scope="request"></c:set>
Do not include ${} in var because it's already variable name, only use ${} when it's for value

<c:remove>
<c:remove var="abc" scope="request"/>
If scope is not set, this will remove all four levels attributes with that variable name

<c:catch>
<c:catch var="myException" >
<% int i=8/0; %>
</c:catch>
<c:out value="${myException}"></c:out><c:out value="${myException.message}"></c:out>
Catch and assign the exception a variable name, so you can catch it latter

<c:if>
<c:if> can only work with single condition
<c:if test="${a=='hello'}">
OK!
</c:if>
<c:if test="${a!='hello'}">
NOT OK!
</c:if>

<c:if test="${age>56}">
older
</c:if>

<c:if test="${age1>10 and age1<56}">
in middle
</c:if>

<br />

<c:choose><c:when><c:otherwise>
Same as:
switch(){
case:
case:
default:
}
<c:choose>
<c:when test="${age>56}">Old</c:when>
<c:when test="${age<29}">Young</c:when>
<c:when test="${age>=29 and age<=56}">Middle</c:when>
<c:otherwise>Age not available</c:otherwise>
</c:choose>


<c:forEach>

<%
ArrayList<User> list = new ArrayList<User>();
list.add(new User("aaa",19));
list.add(new User("bbb",29));
list.add(new User("ccc",39));
pageContext.setAttribute("list", list);
%>
1.Simple forEach
<c:forEach items="${list}" var="user">
<c:out value="${user.name}" />
<c:out value="${user.age}" />
<br />
</c:forEach>
2. iterate fix times, like for loop
<c:forEach var="i" begin="1" end="10">
<c:out value="${i}" />
</c:forEach>
3. iterate fix times with number of steps, like for loop
instance 1 can use `step` also
<c:forEach var="i" begin="1" end="10" step="3">
<c:out value="${i}" />
</c:forEach>
4. can iterator through map or set
<c:forEach items="${map}" var="entry">
key=${entry.key},value=${entry.value}
</c:forEach>

<c:forEach items="${set}" var="value">
value=${value}
</c:forEach>
5. nested level also work fine
<c:forEach items="${set}" var="value">
name=${value.person.name}
departmentName=${value.department.name}
</c:forEach>

<c:forTokens>
<c:forTokens items="a,b,c,d,e,f" begin="0" end="2" step="2" delims="," var="value">
<c:out value="${value}" />
</c:forTokens>

<c:redirect>
<c:redirect url="http://www.google.com"></c:redirect>

<c:import>
import other pages, can also pass parameters, use ${param.name} to get passed value
<c:import url="/WEB-INF/a.jsp">
<c:param name="name" value="abc"></c:param>
</c:import>