Image is not showing in JSP File - java

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/"/>

Related

How to define more than one jsp file in web.xml?

[enter image description here][1] since I'm a newbie to jsp, I'm having lots of doubt buzzing around me.
I need to know how to define more than one jsp file in web.xml.
thanks in advance :).
We do not declare any JSP files in web.xml, we only mention the initial page to load. by default it could be index.jsp or default.jsp. once the default page is loaded the navigation to another jsp will be handled either bu servlet redeirect method or using href with page name to redirect.
Contact us

JSP page included in servlet not rendering satisfactorily

I have two jsp files (header.jspf and footer.jspf) that I want to include in a page produced by the doGet method of a servlet (Menu.java). I'm doing this by use of the RequestDispatcher.include() method. The header.jspf file only contains html so it renders fine. The footer.jspf file, however, contains both EL and JSP tags, neither of which render on the servlet page produced by Menu.java.
Looking at the source code of the page produced by Menu.java I understand that the reason for this problem is that the EL and JSP is not evaluated prior to being included in the servlet so it is just presumed by the browser that it is html.
While I'm guessing that what I'm trying to do may be poor (and deprecated) practice, I'd just like to find out if there is a way to get an included jsp file to render in a servlet just as it would in a jsp page when it contains EL and jsp tags?
I'm thinking that this question is general enough not to require my code to be posted, however if I'm wrong please tell me so and I will update with my code.
Not sure if there is a way to have RequestDispatcher.include() files processed, but including the footer explicitly will very likely work:
<%#include file="footer.jspf" %>

Why doesn't my image show in a jsp? [confused]

I'm new to Spring MVC and the Spring framework in general and I'm trying to learn it.
I did a small "hello world" project using the Spring Tool Suite and encountered a strange issue. So I started a new Spring Project and chose Spring MVC Project. I created a controller and a view jsp page to display a hello page. All is working fine. There is no problem here.
Then I decided to include a picture in my jsp page. I copied a picture to the "webapp/resources" folder and put an img tag like this in my view page (whose path is "webapp/WEB-INF/views/hello.jsp"). The tag looks like this :
When I run the application and go to my view page it doesn't show the image in STS internal browser or in my regular browser. I tried to view the source of the page from my browser and the source does have the img tag but for some reason it's not shown.
I tried to put the image directly in "webapp/WEB-INF/views" along with my jsp file and changed the tag to this:
but still no success. Finally, I blamed this on my inexpirience with creating jsp's and tried to open up my regular Eclipse IDE (i.e. one different from STS) and created a Dynamic Web Project where I inserted the tag into a jsp file and copied the picture in one of the project's subfolders. I ran the project and now it works ; the picture is shown.
Why is happening this? Why it works in a regular web project and doesn't work in a Spring MVC Project? Thank you for your time and I appreciate if you help me!
I tried to put the image directly in "webapp/WEB-INF/views"
The image shouldn't be under /WEB-INF/ because this folder is not accessibile outside the app. It should be under webapp, e.g.webapp/resources/myLogo.png
and then use it like : resources/myLogo.png in the img src.
jsp files are not regular html file.
you should use <c:url /> tag to make sure your resources path are computed from the root of your project:
<img src='<c:url value="/img/mylogo.png"/>' class="logo"/>
where mylogo.png is inside WebContent/img/

How to include an html fragment in a jsp that loads at run-time?

I need to include content in a JSP without using the include directive.
It is easy to do a server-side include with with <%#include file="includeMe.htm" %> but the content of includeMe.htm gets added to the JSP before the JSP is converted into a Servlet by the container. This means that if includeMe.htm gets modified, the changes are not reflected in the generated .java Servlet file. I'm tired of going into Tomcats generated files directory to manually delete the generated java and class files or re-deploy my app every time the included file changes.
Do I need to write a code block to just read in data from the text file line by line and then write it like this?
<%
while( not done reading from file ) {
String line = scanner.nextLine();
response.getWriter().println(line);
} %>
Is there an easier or cleaner way?
Take a look at answers here.
You can also use <c:import>:
2) The <jsp:include> standard action
<jsp:include page="header.jsp" />
Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic
content coming from JSPs. 3) The <c:import>
JSTL tag:
<c:import url=”http://www.example.com/foo/bar.html” />
Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like
<jsp:include>, but it’s more powerful and flexible: unlike the
other two includes, the <c:import> url can be from outside the
web Container!
Regarding reading a file into string. If the file is small do it one line:
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
You can use .tag files for reusable content or do <jsp:include> (INCLUDE dispatch) to another JSP.
However Tomcat is able to re-compile JSPs when any of the included files got changed:
Recompile JSP when included page changes - Jasper 2 can now detect when a page included at compile time from a JSP has changed and then recompile the parent JSP.
Also note, that the common practice is to use .jspf (JSP fragment) extension for files included via <%#include %>.

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)

Categories

Resources