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.
Related
I am attempting to learn Java EE. After struggling through trying to understand the Java stack, it appears that that a basic dynamic web application can be accomplished by focusing on JSP and Servlets so I want to start there.
In Net Beans I have a basic project set up with a tomcat server and when I hit run I can get the web page to generate and display the default index.jsp page. I have added a css file to the project and this jsp page looks like any standard html page now. I then create a new servlet. I was also able to create a link in the index.jsp page so when I click on it, it calls the servlet.
What I don't understand is why when I create the servlet in Net Beans, the processRequest method comes pre-populated with several html print statements. I could easily make this page look like the jsp page I started with, making me wonder why I even need a JSP page. It appears the entire application could be comprised of servlets.
Could someone please explain the proper interaction between the two? It appears the index page can contain links that call various servlets, perhaps to access a database of comments, then do I create the response table in the servlet, or would I pass that information to another JSP page?
Understanding these basic workflows would really help. Thanks
JSP also a kind of a servlet . Right after you run the jsp , go to your netbeans project directory and right click the jsp and select view Servlet then you get the servlet representation of your jsp file. Actually jsp file is there to perform front end design easily. Without jsp It is really difficult task to perform front-end developing stuff inside servlet.
And you should keep in mind there is no such a thing called proccessRequest in javaEE . It is automatically generated by Netbeans once you create a new servlet. So just stick to basics. Erase all the auto-generated stuff from the class which has extended HttpServlet. And then you can override the required HTTP method/methods. (doGet , doPost etc..) You should take a look at this for more infromation ...
And this page will also improve your knowledge on javaEE
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.
I use Spring MVC and AngularJS for my project. Can AngularJs $routing and ngView be used instead of or together with the Apache Tiles framework? As far as I can see using $routing and ngView we create templates and reuse them in a singlePage application.
Yes, AngularJs' routing can be use instead of tiles. Also you can use together, but after it may broke singlepage app idea.
In tiles you probably use jsp files. So jsp files compiling in server-side , and serving as html file in client side via browser.
In angular also you can create base layout. And like tiles' layout extend ability , with ng-include you can include your other views your ng-view htmls. And when route another page ng-view will refresh included area(Also there are some third party plugins/frameworks on AngularJs about it.Take a look ui-router. It provides nice nested views.).
In Angular way commonly no need to get html from serverside(this situation may change depending on the other situations. To make singlepage no need.). So you will need to convert your controllers which returns ModelAndView object to Map(as an example) to return json. At this time routing will be achieved by angularJs' routes. After these refactorings , you will have a singlepage application. Or you can make hybrid-singlepage(some pages have too many event so you van start with that pages to make single page to reduce workload) So It will reveal workload depends on your application's size.
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"/>
I just wants to know that is it possible to integrate Displaytag with spring web mvc ?
If it is possible then can anybody please provide some tutorial or links for that ?
Thanks in advance..
A quick google for "DisplayTag Spring" suggests it works.
From DisplayTag with Spring
There is no extra configuration needed
to use the displayTag with Spring.
Just drop in the jar file to
WEB-INF/lib and start using it in your
jsps
From How to use display tag library in Spring MVC
Place these three jars under
WEB-INF/lib folder displaytag-1.2.jar,
displaytag-export-poi-1.2.jar and
displaytag-portlet-1.2.jar.
Before using it in your jsp you should
have the below line <%# taglib
uri="://displaytag.sf.net"
prefix="display"%>and now you can use
it how ever you want like
note - prefix uri with http.
Btw. a common problem that occurs
with Displaytag + Spring canned
*FormController is that onSubmit is (by default) only called on POST
requests. Keep in mind that displaytag
uses GET requests for sorting and
pagination (as it should), so you may
have to play with overriding the
isFormSubmission method in your
controller, if you want to handle
sorting and pagination in onSubmit