i am using diaply tags library for displaying my tables.But i cant use the <% %>
tagst there.If i try to us it it gives me error.but i can use tag there.
If i try to use followin code in my jsp it give an error sayin shoul hav a matcheing ending tag.
i have follown java code in jsp
List<Course> = (List<Course>)request.getAattribute("crc");
here Course is a class/bean.
can anyone suggest me such library that i can use with struts for auto paging,displaying list in tables,and with other features provided by display tag.I want to use struts and i want the view to look good and yet easy to devlop.that is i want to achieve high class userinterface with littel effortr toward displaying o/p / view.
can anyone provide the example of disploay tag with struts
You can use the name attribute of the table tag (normally like this:)
<display:table name="crc" ...>
</display:table>
To use the crc List as the basis of Javabeans to display.
See http://displaytag.sourceforge.net/1.2/displaytag/tagreference.html#display-el:table for more information about the table tag.
Related
How can I make Spring-MVC output HTML. Right now I have Spring-MVC filling in data in a List like the one below:
<ul>
<li><c:out value="${memberrequest.name}"/></li>
<li><c:out value="${memberrequest.title}"/></li>
<li>District: <c:out value="${memberrequest.district}"/></li>
<li><c:out value="${memberrequest.school}"/></li>
<li><c:out value="${memberrequest.requestor}"/></li>
</ul>
the out put looks like:
But if the data is blank like title, school etc I don't want a line to blank so I was thinking that I could creating this list in the spring code and then pass the html back to the JSP page but it looks like Spring will not let you do it
Returning HTML from your controller defeats the very idea of MVC, because your controller takes over the responsibility of the view.
Instead, use the <c:if> tag within your JSP. More information here: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSTL4.html#wp74001 (note: the first paragraph of this explanation seems to indicate that you should use a scriptlet; it really doesn't, and if you read down you'll see the JSTL approach).
If you really want to do this you can use the escapeXml="false" attribute on c:out like so:
<c:out value="${myHtmlString}" escapeXml="false"/>
But as parsifal says, it's best to construct your HTML in your view if you can.
How do I get the validation messages I set using the struts2 validation:
public void validate() {
addFieldError("user", "Invalid User");
}
I want to get the "Invalid User" and "user" strings from a custom tagLib to print like:
public int doStartTag() {
pageContext.getOut().print( "Invalid User - user" )
}
UPDATE:
The example was a reduced and non-functional usage just explaining how I would like the values to be returned not exactly how I would use it, but here is the use case:
I want to create a custom tagLib that prints the error from struts validation. In a single tagLib I want to verify if the page contains a validation error for a given input then print in the screen the content of the error.
Scriptlet, <logic:*, javascript -> it is all out of question.
If the jsp is meant to hold the structure of the document a taglib with internal specific rules makes much more sense than flooding the page with logic comparisons.
I am not using <s:fieldError /> (or smth like that) because it create an ul and li structure I cant get rid of, even setting the default template as "simple"
Aleksandr M is correct. If you only want to change the markup spit out by the tag, you definitely don't want to create a tag, you only want to override the template that is rendering it. This is quite easy. You will get a copy of the template from the struts core jar, edit it, and put it in your project. Here's a link to the docs:
http://struts.apache.org/2.3.4.1/docs/template-loading.html
Note, depending on what you want to do, you could just get the error messages yourself. They are all exposed as JavaBeans properties on ActionSupport, which your actions should inherit from, and you can reach them with OGNL expressions, such as in the property tag.
Note, ActionSupport implementst these to fullfil the contract of the ValidationAware interface which it implements.
I have a custom tag that has no body at all. I'm trying to programmatically replace the empty body with, for simplicity's sake,
[<c:out value="SUCCESS!"/>]
The goal is to see "[SUCCESS!]" displayed by the JSP which uses the tag, but all I see is "[]" and if I look at the generated source code, I can see that the c:out statement is written on the page between the brackets, but not interpreted.
Is there a common way to achieve this ? The final goal will be to use other custom tags instead of the "c:out" tag. The tags/content will come from a database.
I tried different techniques with SimpleTagSupport and BodyTagSupport but none of those were successfull. In fact I'm not sure if it is technically possible to do it, since, the tag has already been interpreted at that time.. But then how should this be done ?
Server tags (like your custom tag or JSTL tags) get transformed to Java code when the JSP is translated into a servlet. For example, the following JSP code:
<c:out value="FooBar" />
gets translated to something like this inside the servlet:
....
OutTag outTag = (OutTag) tagHandlerPool.get(OutTag.class);
outTag.setPageContext(pageContext);
outTag.setParent(null);
outTag.setValue(new String("FooBar"));
int evalOut = outTag.doStartTag();
....
In your custom tags you can call other Java classes/methods and can write HTML code (not JSP code) to the response.
The [<c:out value="SUCCESS!"/>] is not interpreted because at this level it's just a string that gets written directly to the response.
I new in JSP, i have a problem with JSP
in php i use
$page=$_GET["page"]
for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about
in PHP when i declare $page above and next step i do
Switch($page){
case 1:about
include 'aboutus.php'
case 2:news
include 'news.php'
}
How can i do it ?
How jsp can do the same way php to display multiple page in 1 layout
Use jsp:include.
<jsp:include page="/WEB-INF/${param.page}.jsp" />
And pass ?page=news or ?page=about, etc as parameter. The ${param.page} prints the outcome of request.getParameter("page"). You can prevent direct access to JSP files (by entering URL in browser address bar) by placing JSP files in /WEB-INF folder.
See also:
Basic JSP/Servlet tutorials
Hidden features of JSP/Servlet
How to avoid Java code in JSP
nowadays you use "templates" of Java Server Faces (JSF) for this approach. When you use JSP, you actually don't use the same concept as in PHP. You'd better use the MVC concept. But to answer your question, you could probably achieve this with the include tag http://java.sun.com/products/jsp/tags/11/syntaxref1112.html and control it with JSTL:
http://www.java2s.com/Code/Java/JSTL/JSTLiftag.htm
So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:
<%# tag dynamic-attributes="dynamicAttributesVar" %>
and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:
<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>
However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.
Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:
<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>
Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.
But what I want to know is, how can I make it do what it does for every other JSP user out there?
Just trying to get a badge for answering a four year old question.
I have this problem as well and came across some help at O'Reilly to use JSTL instead of scriptlets.
The original poster could have used this code to get all keys/values:
<c:forEach items="${dynamicAttributesVar}" var="a">
${a.key}="${a.value}"
</c:forEach>
This would get a specific value:
<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do
<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>
or if you must use scriptlets:
Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")
(Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)