Spring mvc layout without tiles - java

In Spring Mvc, beside using tiles, is there any way to decide the jsp layout. I mean think of a design with side menu panel and if I want to apeear each jsp for a relavent side menu, how can I do that without using tiles.
Thanks

You could crate sepatare jsp for each part of your page layout: header, footer, menu, etc.
And use:
<%# include file="yourPageSection.jsp"%>
to include common parts in your pages.

You can include the html or jsp design using the below.....
It works same as the master page of asp.Net......
<%# include file="page.jsp"%>
<%# include file="page.html"%>

Sitemesh is another framework for jsp page layout: http://wiki.sitemesh.org/display/sitemesh/Home

Related

Image is not showing in JSP File

I am having problems displaying an image in my .jsp file.
The image no_image.jpg is located inside the following directory of my Spring MVC application:
SpringProject\src\main\webapp\assets\images\no_image.jpg
I am trying to access it through my .jsp file like this:
<img src="${pageContext.request.contextPath}/assets/images/no_image.jpg"></a>
Project structure (using apache Netbeans IDE 11):
However this does not seem to display the image, does anyone have any ideas why?
Probably the Expression Language is treated as plain text. To tell the JSP engine to process such expressions, add <%# page isELIgnored="false"%> before the <html> tag in your JSP page (at the top, where you have other "%#"-like directives or JSTL imports). Unfortunately, this parameter's value defaults to true.
I have found the solution. For anyone else experiencing this issue, a possible solution is to put the following into your spring configuration .xml file: (Where you define your base package for the project, my one is named spring-config.xml)
<mvc:resources mapping="/assets/**" location="/assets/"/>

JSP Pagetemplate layout

I noticed that on JSF pages i can include templates in my xhtml page like
<ui:composition template="/WEB-INF/tmp/tmp.xhtml">
is there any alternative to this using JSP Pages+ Hibernate?
With plain JSP you can only reuse content with instructions like:
<%# include file="banner.jspf" %>
JSF facelets allow passing parameters to the facelet to do this with JSP you would need an additional framework like struts.

how to create a template with jsp and servlet

let's say I have a site with 10 pages. Every page is called by a servlet. For example, I have the servlet "index" that makes a forward() to "index.jsp".
In my index.jsp i have 2 includes for the header and the footer
...
<jsp:include page="header.jsp" >
home page text
<jsp:include page="footer.jsp" >
...
Now I have 10 pages similar to the index page, I mean I have 10 pages that include a header and a footer.
Let's say I decide to delete the footer: I should edit 10 pages.
What im wondering is if there's something that allow me to use just one page, and show, dinamically, just the "content" of the page (home page, contacts, ecc), keeping in mind that i use a servlet to get every page content (with a forward()).
what you need is Apache Tiles Framework,
Tiles allows authors to define page fragments which can be assembled into a complete pages at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.
It includes configuration file so you can edit in one file alone , so that the changes will be reflected in common for all jsp files
A nice Startup tutorial here

Extended JSP taglib

Is there any extended JSP taglib available? Basically, because of project constraints, we're using a home-grown framework and working with JSPs. We want a taglib that will help us work easily with html forms and form elements and provide some sort of binding. Something like struts html tablib. I'm not sure if we can use the struts taglib standalone?
The best solution I can provide for you is to use a retired tag library from jakarta.
It still works but it is no longer supported or maintained by them.
The link can be found here
The one you will be interested in is the "input" tag library which has tags for the following fields:
form
text
password
textarea
hidden
select
option
radio
checkbox
There should be no problem using Struts with appropriate JAR files in Web App lib folders (put all the Struts JARs in there)

Using a function from one jsp in another jsp

I have a jsp file were the db connection is created and queries can be executed.
I want to call these functions (of connecting to a database and executing query) from another jsp simply to add new record to a database and to stay on a same page.
I read here about an "import" tag but i don't have any packages. Also i'm not allowed to use JSTL.
Little help?
Thanks for considering my question.
You can use <%#include %> directive to statically include JSP fragments.
<%#include file="db.jsp" %>
Writing JSPs this way is however considered poor practice. Java code belongs in a Java class, not in a JSP file. You could for example use a preprocessing servlet class wherein you do the job in doGet() method and have a reuseable DAO class wherein you hide all the JDBC boilerplate code away.
You shouldn't program in your jsp. Put that connection logic out into a class and use that in your jsps.
Afterwards you can import your class with:
<%# page import="package.yourclass" %>
In general you should consider using frameworks like Spring MVC or Struts. Using JSF could be another option.
Mainly we used to import two types of pages into a jsp page,
Java pages
JSP pages
If you want to import a java page into your jsp page, syntax is
<%# page import="package_name.java_page" %>
If you want to import a jsp page, syntax is
<%# include file='jsp_page_name.jsp'%>
while adding a jsp page, if it is in an another folder, you just need to specify the paths also.

Categories

Resources