Reputation: 1030
I'm dabbling into JSP after having experienced most of life as a PHP developer. I'm quite comfortable with Java as it is, so I've started to get to know Struts as my MVC framework of choice.
I'm having trouble understanding the reasons why I should use the Struts taglib instead of standard HTML?
<s:textfield id="username" />
or
<input type="text" id="username" name="username" />
What's the conceptual difference? Why should I use the taglib rather than HTML? Is there a preferred method within the world of Struts and the JSP community at large?
Upvotes: 2
Views: 4429
Reputation: 50281
There is no way to access server data with pure HTML
; the alternative to TagLibraries
inside a JSP
is not the HTML
, but the old (and bad) Scriptlet
s.
Everything at the end will be transformed to HTML
, but you need an intermediate tier to allow you handle business data.
How can you read serverside values with HTML
?
Long story short:
once was the Servlet
, with its PrintWriter
s to output HTML
:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String myServerSideValue = "StackOverflow";
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("<title>Wait, what... a Servlet in 2013?!</title>");
writer.println("</head>");
writer.println("<body>");
writer.println("<span>Hello " + myServerSideValue + "</span>");
writer.println("</body>");
writer.println("</html>");
}
Clearly a nightmare, that led some smart guy to invent JSP
s and Scriptlet
s:
<html>
<head>
<title>Wait, what... Scriptlets in 2013?!</title>
</head>
<body>
<span>Hello <%= myServerSideValue %> </span>
</body>
</html>
Then TagLib
s like JSTL
came out:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>you <i>may</i> still needs JSTL with, for example, Spring MVC</title>
</head>
<body>
<span>Hello <c:out value="myServerSideValue" /> </span>
</body>
</html>
And finally WebWork / Struts
brought OGNL
to us
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>you still needs JSTL with, for example, Spring MVC</title>
</head>
<body>
<span>Hello <s:property value="myServerSideValue" /> </span>
</body>
</html>
Using OGNL
inside Struts2
tags is similar to JSTL
, but more powerful and fully integrated in Struts2
in several ways (Validation
, Theming
, etc).
Maybe this example is too nàive to understand why Struts2 Tags
should be used instead Scriptlets
or other TagLibs
, but when coming to more complex, real-world scenarios, you will start to appreciate and maximize the usage of this great tool.
Upvotes: 0
Reputation: 160311
Struts 2 tags serve two primary purposes:
Struts 2 uses "themes" to determine what is output for view-oriented tags. The default "xhtml" theme, for example, wraps the input tags in table rows, sets the input field value, displays field validation errors if any, and probably a few other things I'm not recalling.
If you look at the page source you can see precisely what the differences are, except for the bits that are determined by back-end functionality like the if/then around validation messages.
In general, custom tags allow you to abstract view-layer functionality in arbitrary ways. The "preferred method" is to not do stuff by hand, which is what you'd need to do in order to duplicate even the simplest functionality like setting the value from the action and displaying error messages.
You can see what the custom tags do by looking at their FreeMarker templates (assuming you're not using the Java tags). You can extend those templates, create your own templates, etc. all under the framework's purview.
Even a cursory examination of the S2 tag and theming docs should answer this question.
Upvotes: 3