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/
Related
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/"/>
I have some CRUD app that connected through hibernate with the database. There is one JSP file in views. Everything is configured in the config XML file. But CSS still doesn't work.
My app running on IntelliJ IDEA 2019.1.2, MySql 8, Spring 5, Tomcat 9. I tried a few different ways to set up in JSP file path to CSS files. But the result always the same. Everything working, but without styles.
servlet config
project structure
jsp file
result
In the png you it seem you are using resourses instead of resources. Also verify in developer tool of your browser the name of resources folder. You can go to sources tab(chrome) or debugger(firefox) and check the folder structure.
I'm working with something called JasperReports, and in my servlet, there's this piece of code:
JasperExportManager.exportReportToHtmlFile(jasperprintobject,"C:\\Users\\user\\Desktop\\reports\\myreport.html");
This creates an html file called myreport.html in the reports folder on my desktop, and it also creates a folder called myreport.html_files containing images that myreport.html points to, also inside the reports folder.
I would like to serve this content to the user.
Unfortunately, it seems that the exportReportToHtmlFile method won't let me put the html file inside WEB-INF.
If I could, I would just do:
request.getRequestDispatcher("/WEB-INF/myreport.html").forward(request,response);
I'm thinking about creating a jsp page that can somehow include the html file inside it and forwarding the user to the jsp page, but I'm not sure how to do this.
This is a dynamic web project in eclipse and I'm using tomcat 8.5
Does anybody have any ideas?
Thanks.
I have a spring web project which basically Test webApp and capture screenshot of pages. The path of the saved image looks something like below:
"\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
\Demo\WEB-INF\Results\Test\1118015800\error\error_ST001_1.jpg"
I am trying to display the saved image on a JSP page and so far success has been elusive. I tried different combination of relative path also I tried to give absolute path but it doesn't work. My image tag looks something like this
<img src="<c:url value="/WEB-INF/Results/Test/1118015800/error/error_ST001_1.jpg" />" />
Does anyone have any idea on displaying the image? Could it be because files are stored inside the .metadata folder of Eclipse workspace that I am not able to display any image?
This is because the WEB-INF directory is special. The web container won't serve files from that location - that behavior is defined in the Servlet Spec.
You can either create a servlet that takes requests from the client, loads images (e.g. using getResourceAsStream()) and streams them back, or move the images to a different place in your web app hierarchy.
I want to display a static html page embedded into a play 2.2.1 project.
I have created a play 2.2.1 project.
I have put my whole static html files with its assets(js, css, img ect.) into the public folder of play.
I also have deleted the precreated controller and views packages.
My routing file looks the following:
GET /comingSoon.html controllers.Assets.at(path="/public", file="comingSoon.html")
However, when I go to http://localhost:9000/ I get:
I really appreciate your help!
Hm, I don't get it, actually Play told you that you didn't create any route for path / ;)
just add this route:
GET / controllers.Assets.at(path="/public", file="comingSoon.html")