Spring 3 - Theme with separate JSP - java

I'm trying to rewrite some Spring 1.2 code to Spring 3.0 one. Currently I'm stuck with JSP resolved by URL problem. Application uses separate JSP files with different layouts for serving the same model from the same controller. The JSP is switched using interceptor, that intercepts the url and changes the view.
For example:
/design_one/mypage.htm -> MyPageController -> /design_one/mypage.jsp
/design_two/mypage.htm -> MyPageController -> /design_two/mypage.jsp
Is there a way to make same or similar functionality using something better than raw interceptors?

Well, I've found a way to do it, but not sure if it's the optimal way.
What I did, I defined a name for each theme in their resource bundle, like "name=design_one". And then using it in JSP to include some other JSP's inside.
Something like this:
<spring:theme code="theme.name" var="themeName"/>
<jsp:include page="${themeName}/head.jsp"/>
<jsp:include page="${themeName}/foot.jsp"/>

Related

Spring MVC, how to include another controller/view inside a view (partialview)

I'm just starting out with Spring MVC having been trying to pickup Java(Web) for the few months.
I'm sure what I'm trying to achieve is very simply, but I can't find a solution that really works.
I'd like to include another controller/view inside a view. This could be a menu, or some other dynamically generated content. For example, let's say in a side bar of an application I need to display a list of categories. I don't want to have to retrieve these categories in every controller/view that needs to use them. I'd like to include another controller/view into the main page view so that this content can be placed in multiple views but managed by a single piece of code.
ASP.NET MVC has something called PartialViews, and most PHP frameworks seem to offer this kind of functionality, but I can't find anything like this in spring.
The closest I've come to resolving this issue is to use the jsp:include tag, which does work, infact it's almost the solution, but it generates errors in Eclipse because obviously Eclipse can't locate the path, as it's a spring RequestMapping i.e. /include/categories rather than a direct link to a physical file.
The other solution is to use Javascript to dynamically load content into the sidebar, but I don't want to do that.
Is there a 'correct' way to do this, or is there a way to supress the errors generated by eclipse for the jsp:include tag?
If the content your are including is static and you don't have a lot of different pages, your approach with including jsps is ok. Otherewise, look at the templating frameworks like Tiles or Thymeleaf.

Does Spring MVC 3.x support partial views (html/jsp) like that of ASP.NET MVC3/4?

Q1: Does Spring or any opensource java UI framework support partial views like that in asp.net mvc?
For example in my main index.html (or _layout.cshtm per asp.net mvc3 spec)
I would have the folllowing code:
<span id="logindisplay">#Html.Partial("_LogOnPartial")</span>
where #Html is a helper to display a partial view for _LogonPartial.cshtml which just injected it's html view contents into the page?
Q2: If this is supposed If I want to display a bunch of partial views it would be helpful to display them concurrently in parallel to improve performance. Very similar to what linkedin is doing using dust and fizzy? http://engineering.linkedin.com/profile/engineering-new-linkedin-profile
Q3: Is fizzy available as open source like that of dust?
If you want to include content of a page into another page, by adding some code on the page itself, you should compare asp with jsp, not ASP.NET MVC* with JEE - Spring MVC
So, an equivalent to <span id="logindisplay">#Html.Partial("_LogOnPartial")</span> on a jsp would be one / all of the following
On your jsp, include content from another jsp using <%# include file="../includes/inner-content.jsp" %>. This is what is called a static include. The source of the included jsp is included and made part of the parent jsp, before the jsp is compiled. If you use an IDE, it will check to ensure the included jsp does infact exist at the path specified, relative to the location of the jsp in which you are adding the include. Technically this is a JSP Directive. The jsp being included could just be a fragment, and not addressable from the outside world (could be hidden inside WEB-INF)
You can also use what's called a Dynamic include <jsp:include page="someJSP.jsp" />. In this case, the included JSP should be addressable from the browser and should be capable of being rendered independently. When the server is executing the servlet to render the parent JSP, it stops when this tag is seen, and starts executing the servlet for the included jsp, the output obtained from the inner jsp execution is then merged to the output of the parent jsp, and processing of the parent jsp is resumed.
A third option would be to use Core JSTL taglib's <c:import url=""/>. This works just like option 2 above, except it also allows you to import a page / content from a url that lives outside your application. Basically you can mention a path to a jsp, or relative URI to a servlet mapping in your application, or a URL to an external page.
Now, I suspect this is not really what you want to do, if you are comparing with what Linkedin is doing. You want to mashup content from sources in your own application, and compose your page. You also want to do this in an asynch manner so as to keep load times in check. In this case, you HAVE to use JavaScript and Ajax. All the mechanisms described above are for server rendered pages (All HTML is created before the page is rendered in the browser). Just like #HTML. You need to create a simple framework / use an existing one, where once a page loads, it fires asynch ajax calls to the server to get content for specific areas on the page and renders the returned HTML in those specific areas.
Hope this helps.
Do let me know if I've misunderstood your question.

JSP directory structure

I've been starting doing some Java and JSP and now, I'm stuck. I'm not happy with having all static HTML's in the root-folder. If I would pack those into a separate folder. At the moment, I see two options: configure it in the web.xml, but that only works for the servlets afaik. Configuring Tomcat on the other hand might be an option, but it would break the internal logic, even if it worked.
Is there a correct way for organizing the files and not having that reflected in the URL?
When you speak about static pages something like say header and footer.
You can place them under any subfolder say inside jsp/static/(subcontent) or depending on the type of your file you can group them even further.
Now the usage, since because you are with plain jsp and servlet the only option that I think makes sense for static content is that include them whereever required with include directive like:
<%#include file="/pages/static/abc.html" %>
or forward the request to them with
<jsp:forward page="/pages/static/staticdata.html" />
Understanding that this is a pain, if the navigation needs to be changed you will have to change your code, then more advanced web framework like Struts, JSF, e.t.c makes sense, as navigation rules can be changed very easily without changing code (unless the flow causes model to change or any different scenario).
Along with navigation rules these framework also specify page building configuration like tiles in Struts, templates in JSF e.t.c
You can actually map jsp files to urls in web.xml using
<jsp-file></jsp-file> tag
Example
<servlet>
<servlet-name>somName</servlet-name>
<jsp-file>newFolder/mypage.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>somName</servlet-name>
<url-pattern>/oldUrl.jsp</url-pattern>
</servlet-mapping>

Spring MVC. Rendering layout

I'am new to Java Spring MVC. Official documentation is very poor and i've got a lot of questions about rendering pages in Spring.
I have page and top block with menu. This menu must be dynamic and must be displayed on all pages. In PHP frameworks this is very common and very easy task. But Spring makes my brain explode.. How can i separate rendering of this menu in some peace of logic and tell spring to render it all the time? For example in JSP. Shoud I use beans or other stuff?
Thanks for your answers!
Official documentation is very poor
No it isn't. Its the one place I always refer back to.
I use a jsp include and yes pass a java bean to the jsp from the controller. The included jsp will be cached the first time it is included. In other places I connect to a controller using jquery and ajax, which dynamically updates the page.
I'd suggest the official tutorials, and krams as good starting points.
You won't find much information about this in the Spring documentation because it's out of the scope of Spring.
You can use JSP includes just as you would do PHP includes. This will however force you to repeat the page layout and the include in each and every page of the application :
<jsp:include page="/menu.jspf" />
Or use a templating engine, like Sitemesh or Apache Tiles (see http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/view.html#view-tiles for Tiles integration in Spring).
The easiest way would be to use <jsp:include page="/menu.jsp" /> where "/menu.jsp" is a path to your menu jsp.
There are other solutions like using tiles, but <jsp:include> should do.

Use jsf within jsp (change portion of jsp to use jsf)

I need help about converting a portion of jsp page to jsf. My old jsp page is big, and I am trying to change a portion of it (CountryDropdown List) to JSF. So I created a countryDropdown.jsf, I tested to run it directly, it loaded the country-list/works fine.
In my old jsp page (company.jsp), I removed the countryList's portion and try to call/include the new jsf "countryDropdown.jsf" something like this:
<jsp:include page="/web/Test/countryDropDown.jsf" flush="true" />
But I get the errors: java.lang.RuntimeException: Cannot find FacesContext
I am not sure what I was missing, if I directly run the new jsf page http://.../web/Test/countryDropDown.jsf it works fine.
It's not possible to include a JSF page in a regular JSP called directly. If you want to use JSF components, your request must be dispatched by the FacesServlet.
It might be possible to get your setup to work if company.jsp is itself called as a JSF page. But you'll probably keep running into problems; JSF is not really meant to be introduced in bots and pieces into an existing application. At the very least, each page should be either completely JSF or completely classic JSP.

Categories

Resources