How to determine which Tile definition a JSP page uses? - java

I'm pretty new to Java, and just encountered a strange problem. Previously, everything was working fine and I don't know what I did to screw it up (I know, everyone says that). What's happening is that I am working on a web app that uses Apache Struts (1.x) and Tiles, and when I go to a page in the web browser, it seems to be using a different Tile definition than it used to.
Here's the relevant lines from tiles-config.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration//EN"
"http://supplies.ur.com/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name=".default" path="/template_new.jsp">
<put name="title" value="[Company Web App Name]" direct="true"/>
<put name="customerInfo" value="/customerInfo.jsp" direct="false"/>
<put name="smallBody" value="" direct="true"/>
<put name="menu" value="" direct="true"/>
<put name="quickMenu" value="/quickMenu.jsp" direct="false"/>
<put name="adminMenu" value="/adminMenu.jsp" direct="false"/>
<put name="message" value="/message.jsp" direct="false"/>
<put name="body" value="" direct="true"/>
<put name="helpAvailable" value="yes" direct="false"/>
<put name="feedbackAvailable" value="yes" direct="false"/>
<put name="problemReportingAvailable" value="yes" direct="false"/>
<put name="newFeaturesAvailable" value="yes" direct="false"/>
</definition>
<definition name=".empty" path="/blank.jsp">
<put name="title" value="[Company Web App Name]" direct="true"/>
<put name="customerInfo" value="/customerInfo.jsp" direct="false"/>
<put name="quickMenu" value="/quickMenu.jsp" direct="false"/>
<put name="message" value="/message.jsp" direct="false"/>
<put name="body" value="" direct="true"/>
<put name="footer" value="/footer2.jsp" direct="false"/>
<put name="helpAvailable" value="yes" direct="false"/>
<put name="feedbackAvailable" value="yes" direct="false"/>
<put name="problemReportingAvailable" value="yes" direct="false"/>
<put name="newFeaturesAvailable" value="yes" direct="false"/>
</definition>
<!-- Forgot to include the .rentalHistoryByType definition... -->
<definition name=".rentalHistoryByType" extends=".default">
<put name="title" value="Equipment Rental History By Type" direct="true"/>
<put name="smallBody" value="/rentalHistoryByType_Search.jsp" direct="false"/>
<put name="body" value="/rentalHistoryByType.jsp" direct="false"/>
</definition>
<!-- Added for CoolBeans -->
<definition name=".customerChoose" extends=".default">
<put name="title" value="Choose a customer" direct="true"/>
<put name="quickMenu" value="" direct="true"/>
<put name="smallBody" value="/customerChoose_Search.jsp" direct="false"/>
<put name="body" value="/customerChoose.jsp" direct="false"/>
</definition>
</tiles-definitions>
Here's the relevant lines from struts-config.xml:
<action path="/rentalHistoryByType" type="com.[company name deleted].etrieveit.actions.RentalHistoryByTypeAction">
<forward name="customerNotSelected" path="/customerChoose.do" />
<forward name="success" path=".rentalHistoryByType" />
</action>
<!-- Added for CoolBeans -->
<action path="/customerChoose" type="com.[company name deleted].etrieveit.actions.CustomerChooseAction">
<forward name="success" path=".customerChoose" />
</action>
The page I'm going to in the browser is "[path]/rentalHistoryByType.do", the body of which is built with rentalHistoryByType_Search.jsp and rentalHistoryByType.jsp. The way I found the problem is that jQuery suddenly stopped working on the page when it had been working fine before. On viewing source, I found that rentalHistoryByType.do is being built from blank.jsp, not template_new.jsp. (template_new.jsp has the script includes for jQuery, whereas blank.jsp does not, and other details in the page source match blank.jsp exactly as well.)
My question is, how can I tell which Tile definition a page is going to use when it gets requested and how can I change it? And/or, what might cause a page to use a different Tile definition than it was apparently using before?
Sorry if my terminology is wrong or confusing, I'm new enough to Java that I don't really know what I'm doing yet...

There should be a struts config file (named struts-config.xml typically) that has a mapping of actions to definitions, e.g. it would contain
<action path="/home"
type="com.myco.MyHomeAction">
<forward name="home/landing"
path="home.definition"/>
</action>

Found the answer; it was apparently a caching issue. I stopped JBoss, deleted the contents of JBoss's \server\default\tmp and \server\default\work folders, and restarted JBoss, and now it's working correctly...

Related

how to add dynamic value in apache tiles from controller in spring mvc

tiles
<definition name="home" extends="container">
<put-attribute name="title" expression="${modal.title}" />
<put-attribute name="body" value="/WEB-INF/jsp/masters/home.jsp" />
<put-attribute name="view" value="home" />
</definition>
this is my method in controller
#RequestMapping("/home/{linkAddress}")
public String homeDt(
#PathVariable(value="linkAddress") String linkAddress,Model model){
HomeMt homeMt = homeMtService.findByLink(linkAddress);
model.addAttribute("contentOne", homeMt.getContentOne());
model.addAttribute("title", "ronaldo");
model.addAttribute("homeMt", homeMt);
return "home";
}
my problem is that the title value is not coming in the jsp page,
some say that enable the el support but it didn't worked for me.
any help will be appreciated

JSP markup inside <tiles:put> or a or an alternative

I've inherited an old application that is using Struts 1.2 Tiles 1. For various annoying reasons I can't upgrade.
I'm not very familiar with Struts, or specifically Tiles in general and I'm trying to do something that makes sense in my head but I can't seem to make work in practice. Here's an example of what I'm trying to accomplish:
<tiles-definition>
<definition name="content-with-sidebar" path="/content_with_sidebar.jsp">
<put name="top" value="" type="string" />
<put name="sidebar" value="/tiles/sidebar.jsp" />
<put name="main" value="" type="string" />
<put name="bottom" value="" type="string" />
</definition
</tiles-definition>
content_with_sidebar.jsp
...
<tiles:insert attribute="top" flush="false" />
<div id="content">
<aside>
<tiles:insert attribute="sidebar" flush="false" />
</aside>
<div id="main">
<tiles:insert attribute="main" flush="false" />
</diV>
</div>
<tiles:insert attribute="top" flush="false" />
...
actual_page.jsp
<tiles:insert definition="content-with-sidebar" flush="false">
<tiles:put name="top" type="string">
<div>Maybe this page has something on the top that isn't the page header</div>
</tiles:put>
<!-- use the default sidebar -->
<tiles:put name="main">
<strong>Current Location:</strong>
<address><h:outputText value="#{locationDesc} #{zipCode}" /></address>
<!-- Some more dynamic jsp markup -->
</tiles:put>
<!-- This one doesn't have anything extra on the bottom -->
</tiles:insert>
This almost works but the dynamic bits get rendered above and outside the <tiles:insert> and the plain strings go where they should. I understand now, after much searching, that <tiles:put> in this, er, context, is expecting a plain ole string.
Is there a pattern to accomplish what I want with dynamic context?
As it stands I'm having to create another jsp file to be referenced by the <tiles:put> tag. i.e.
<tiles:put name="main" value="/actual_page_body.jsp" />
I'd rather not have to create an additional file when one would do. Any advice would be helpful.
With tiles, when the definition is rendered (content-with-sidebar in your code), the jsp corresponding to the 'path' attribute (/content_with_sidebar.jsp) will be invoked. All other jsps have to be manually invoked using a <tiles:insert>. What tiles provides you is a way to configurationally invoke them as definition attributes.

which one of these is the better approach of using tiles

Problem: I have two identical pages home.jsp and contact.jsp with same structure. They differs only in body content and title. I want to create a Layout page using tiles framework and reuse the code for the two JSPs. The controller framework is yet not decided, it may be Spring MVC 3 or Struts 2.
Solution A: Calling JSP files/views directly from controller/action classes.
I write a single definition in tiles.xml like:
<definition name="baseLayout" template="/WEB-INF/jsp/layout/baseLayout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/jsp/includes/header.jsp"/>
<put-attribute name="body" value="" />
</definition>
Now in baseLayout.jsp:
<html>
<head><title><tiles:insertAttribute name="title"/></title></head>
<body>
<div class="wrapper">
<div class="header"><tiles:insertAttribute name="header"/></div>
<div class="body"><tiles:insertAttribute name="body"/></div>
</div>
</body>
</html>
Now in home.jsp
<tiles:insertDefinition name="baseLayout">
<tiles:putAttribute name="title">
Title for home page
</tiles:putAttribute>
<tiles:putAttribute name="body">
Content for home page
</tiles:putAttribute>
</tiles:insertDefinition>
Similarly for contact.jsp :
<tiles:insertDefinition name="baseLayout">
<tiles:putAttribute name="title">
Title for contact page
</tiles:putAttribute>
<tiles:putAttribute name="body">
Content for contact page
</tiles:putAttribute>
</tiles:insertDefinition>
Solution B: Calling the tiles definition of different JSP files from cotrollers/action classes. This approach requires one tiles definition for each of the JSP file I would be writing. So altogether 3 tiles definitions (one is for baseLayout and other two are for home and contact).
tiles.xml :
<definition name="baseLayout" template="/WEB-INF/jsp/layout/baseLayout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/jsp/includes/header.jsp"/>
<put-attribute name="body" value="" />
</definition>
<definition name="home" extends="baseLayout">
<put-attribute name="title" value="Title for home page" />
<put-attribute name="header" value="/WEB-INF/jsp/home.jsp"/>
</definition>
<definition name="contact" extends="baseLayout">
<put-attribute name="title" value="Title for contact page" />
<put-attribute name="header" value="/WEB-INF/jsp/contact.jsp"/>
</definition>
baseLayout.jsp : Same as **Solution A**
home.jsp : Content for home page
contact.jsp : Content for contact page
I want advice on which one of the above approaches I should stick to.
The second solution is the best way:
You can have independent section in your tiles properties for each
jsp layout.
You can change anytime without effecting other layouts later on
The most traditional way using in struts
First solution:
May cause problems later on with having to edit them with any change
specially when you are deep in the project in advanced levels
More generic approach not suitable for struts/tiles architectural
designs
Solution B is the best approach to implement,
you will have individual style template what you can reuse later if required for other jsp's without messing the code in jsp like Solution A.
As of understanding the second approach is more clear and a common standard to follow.
Solution A helps maintenance in large applications by distributing configuration and also promoting name convention over explicit configuration. Moreover it is conceptually compatible with other templating solutions such as JSF or plain tags. For example see this: JSP tricks to make templating easier?
Think an application with hundreds of controllers and views written by different people over a large period of time. With solution B you will have to face a huge tiles.xml file. Add a slight lack of discipline and view names end up an inconsistent mess and content present inside the configuration (like your example).

Can I access the Apache Tiles definition name inside a ViewPreparer?

Is it possible to access the Apache Tiles definition name inside a ViewPreparer?
just add the attribute into the xml, eg
<definition name="t.*" extends="t.container">
<put-attribute name="body" value="/WEB-INF/jsp/{1}.jsp"/>
<put-attribute name="title" expression="${song.title} - ${song.author} in ${jukebox.name}"/>
<put-attribute name="view" value="t.{1}"/>
</definition>

Struts tiles - a simple putList causes parse error

It has been a while since I did something in tiles. I am trying to build a simple website with some "bread crumb" feature. I would use static bread crumbs as my page navigation is pretty limited. Here is my tiles-defs.xml file:
<tiles-definitions>
<definition name=".template" path="/pages/template.jsp">
<put name="title" value="Template"/>
<put name="header" value="/pages/common_header.jsp"/>
<putList name="breadcrumb"/>
<put name="body" value="/pages/sample.jsp"/>
<put name="footer" value="/pages/common_footer.jsp"/>
</definition>
<!-- staring page for the application -->
<definition name=".start.page" extends=".template">
<put name="title" value="Server details"/>
<putList name="breadcrumb">
<item value="Home" link="#"/>
</putList>
<put name="body" value="pages/home.jsp" type="page"/>
</definition>
</tiles-definitions>
Just by introducing <putList name="breadcrumb"> the tiles-defs refuses to parse: I keep getting the following error:
SEVERE: Parse Error at line 45 column 18: The content of element type "definition" must match "(icon?,display-name?,description?,put*,putList*)".
org.xml.sax.SAXParseException: The content of element type "definition" must match "(icon?,display-name?,description?,put*,putList*)".
This is really a head-scratcher. Any idea what is going wrong?
Using Struts 1.3.8 JARs
It is complaining about the order of the elements. You have put first the "put" elements and after all of them the "putList" ones.
So the code would be something like this:
<tiles-definitions>
<definition name=".template" path="/pages/template.jsp">
<put name="title" value="Template"/>
<put name="header" value="/pages/common_header.jsp"/>
<put name="body" value="/pages/sample.jsp"/>
<put name="footer" value="/pages/common_footer.jsp"/>
<putList name="breadcrumb"/>
</definition>
<!-- staring page for the application -->
<definition name=".start.page" extends=".template">
<put name="title" value="Server details"/>
<put name="body" value="pages/home.jsp" type="page"/>
<putList name="breadcrumb">
<item value="Home" link="#"/>
</putList>
</definition>
</tiles-definitions>

Categories

Resources