Cannot access struts action data from nested JSP - ajax

I am working for first time with Struts 2 + Spring + Hibernate architecture. I took this project as reference for building it and it works. I can list DB tables in my index.jsp using struts tags, but this does not work from nested JSP loaded into DIV containers inside index.jsp.
index.jsp has a <div class="art-nav"></div> and loads there another jsp using js:
$(".art-nav").load("./menu.jsp");
The same struts tags that work in index.jsp to list DB tables do not work in menu.jsp. I am not sure if the problem is the way I am loading this JSP or if it is necessary to execute some action from Struts 2 before loading menu.jsp...
Basically the JSPs use AJAX and I am adapting them to this architecture and there is where I am facing the problems because of my lack of experience.
Thank you in advance for your help!

You should include the second jsp as follows:
<div class="art-nav">
<%# include file="/WEB-INF/jsp/menu.jsp"%>
</div>
If you load your jsp via javascript the response will not get forwarded to that jsp.
If you really want to load a jsp via javascript you should create a new action (menu.action) that returns a parsed jsp and include it in the existing html. (Though I personally do not really like that technique.)

Related

c:import jsp page with translation

I'm trying to load a jsp from remote during runtime. <c:import> accepts remote url and loads the file but it displays the jsp content as it is. Downloaded jsp has custom tags in it which needs to be processed. Library of Tag handlers for the custom tags is added as dependency.
Is there a way to achieve html translation for imported jsp file?

how to include page in other page J2ee Spring MVC2

I need to include page in other page on my project Spring J2ee,
the page whitch must be included has beans to be displayed...
anyone has idea please?
using include directive
<%#include file="/WEB-INF/jsp/myDir/myPage.jsp" %>

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

c:import loads different view than the one specified in tiles

Using:
Spring Web 3.0.5
JSTL 1.2.0
Apache Tiles 2.1.4
Resin 3.1.9
template1: imports jsp for url1
main jsp: imports /my/simple/url2
imported jsp: jsp page without imports nor includes
tiles:
logical view for url1: url1.view (extends tempate1)
logical view for url2: url2.view (is a jsp page)
Now here's what's happening:
When accessing /my/simple/url1: goes to a controller and command which then results to
rendering a template (template1), that imports main jsp
Upon seeing import for /my/simple/url2, dispatches a request for this url
This, in turn, goes to another controller using another command and renders another jsp (aka: imported jsp)
After execution of controller for url2, I expect that url2.view will be rendered and appended to main jsp. But instead of this, template1 is again rendered which results to a loop.
Did anyone experience this problem before? I'm not really sure what's happening.
Ok, so I'm not sure that this will work for for earlier versions of Tiles and Spring (currently using Spring 3.1 and Tiles 2.2.2), but here it goes anyway.
I realized that for some reason, when you do an import using the core tag library within a tiles template, and let's say that import in turn calls a Spring MVC controller, it will cause an infinite loop. The way that I got around this was by doing the following:
Add an attribute in the tiles definition of the layout that will reference a jsp containing the code to do the import. Let's say for example:
<definition name="cti.layouts.fooBarLayout" template="/WEB-INF/views/layouts/foo-bar-layout.jsp">
<put-attribute name="body" value="/WEB-INF/views/some-body.jsp"/>
<put-attribute name="foo" value="/WEB-INF/views/my-import.jsp"/>
</definition>
In this example, you want to add the import code inside of my-import.jsp. You can use the core JSTL tag <c:import>
Inside of foo-bar-layout.jsp, add in a tiles:insertAttribute tag wherever you want this imported page to go. Reference the name of the attribute (in this case 'foo'):
<tiles:insertAttribute name="foo"/>
Now you can extend from this layout without worry about an infinite loop. Not sure why this is working as I don't understand what the underlying implementation is doing, but for some reason, tiles does not allow a dynamic import to be used inside of a template page.
Hope this helped.
I had the same problem and resolved by using the absolute URI of the tile. This triggeres a 'clean' request to the server and isn't aware of the current tile rendering.
I used the import because I required a more dynamic url.
So for the sample case:
<c:import url="/path/to/the/import/jsp/or/controller"/>
becomes
<c:import url="http://localhost:8080/mycontext/path/to/the/import/jsp/or/controller" />
Of course you need to update the baseURI to your applicable situation.
This avoided the recursion happening when combining tiles with c:import
Beware: This triggers a new HTTP-request to your server.

How to develop JSP/Servlets Web App using MVC pattern?

I'm developing a JSP/Servlet web app (no frameworks). I want to use MVC pattern. I am going to design my project like this:
Controller: a servlet that reads a request, extracts the values,communicates with model objects and gives information to a JSP page.
View: JSP Pages.
Model: Java Classes / Java Beans .. etc.
The problem: Index.jsp is the starting point (default page) in my web site. So, the Index.jsp becomes the controller to parse the request. For example, the following request:
index.jsp?section=article&id=10
is parsed in index.jsp as following :
<div class="midcol">
<!-- Which section? -->
<%String fileName = request.getParameter("section");
if (fileName == null) {
fileName = "WEB-INF/jspf/frontpage.jsp";
} else {
fileName = "WEB-INF/jspf/" + fileName + ".jsp";
}
%>
<jsp:include page='<%= fileName%>' />
</div>
Here, I can't force the servlet to be a controller, because the index.jsp is the controller here since it's the starting point.
Is there any solution to forward the request from index.jsp to the servlet and then go back to index.jsp? Or any solution that achieves the MVC goal - the servlet should be the controller?
I'm thinking of making a FrontPageController servlet as default page instead of index.jsp, but I don't know if it's a perfect idea?
Get rid of index.jsp and just let the controller servlet listen on a specific url-pattern of interest. The controller itself should forward the request to the JSP page of interest using RequestDispatcher.
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
Alternatively you can let index.jsp forward or redirect to an URL which is covered by the controller servlet which in turn shows the "default" page (which seems to be frontpage.jsp).
That said, in a correct MVC approach, you should have no scriptlets in JSP files. Whenever you need to write some raw Java code inside a JSP file which can't be replaced reasonably by taglibs (JSTL and so on) or EL, then the particular Java code belongs in any way in a real Java class, like a Servlet, Filter, Javabean, etcetera.
With regard to the homegrown MVC approach, you may find this answer and this article useful as well.

Resources