include css inside a spring mvc java project - java

How is this achieved, I have everything mapped from / to the dispatcher Servlet and the css is in the webapp folder. when clicking on the css link href in the source I get a tomcat error.
Do I need to create a #RequestMapping for css?

Css are served without the need of dispatcher (unless you configure it to serve css). Just put them in some folder in your webapp dir and reference them as /YourAppContext/path-to-your-css in your pages.
For example in typical (non-maven) webapp it's inside WebContent/css
Assuming your app's context name is MyApp and you want to reference WebContent/css/main.css
you should put something like this in jsp:
<c:url value="/css/main.css" />
or uglier using
/MyApp/css/main.css
as your css url

CSS are client files, not server files. All client files (CSS, Javascript, etc.) have nothing to do with Spring MVC, which acts on servlets (on the server). SO you add your CSS exactly the same way you would if you weren't using Spring MVC.

Related

Using HTML,JS and CSS in spring mvc application

I dont want to use JSP and instead use HTML,CSS and JAVASCRPIT .
I dont't want to use any front end technology like AngularJSbut only only AJAX for communicating with backend. I have only one page as index.html and few .js and .css files. But I am not able to configure spring mvc to use these html and other js and css files.
Can somebody help me with the project structure and the configurations for it.
Thanks.

Spring Boot static content url mapping

Is anybody here who know how to create a mapping file from static directory to respond for certain url in Spring Boot?
For example, I have file in directory /resource in Spring Boot structure
resources/static/html/index.html
and I want it to respond to url
/index
No catalogue path, no .html postfix
File index.html is served as default HTML file from certain directory. So if you want to serve it from http://domain:port/index URL, you should place it into resources/static/index/index.html.
As I research more, url mapping is possible only if You use template engine like Thymeleaf and then application will became context aware.
Use Thymeleaf and then add your .html files to resources/templates/ and they will be discovered automatically.

Web Application directory mapping

I'm working on a small web application that contains some JSPs, Servlets and static HTML pages. There are also some filters to implement a small authentication/authorization mechanism. My questions are:
How can I map an entire directory of JSPs to another? Here's an example: I want to map all the URLs like localhost:8080/WebAppName/pages/*.jsp to localhost:8080/WebAppName/*.jsp . How can I do that without writing an entry in the web.xml for each JSPs page in the directory 'pages'?
After having mapped these URLs to new ones, what do I have to write in the tag of in the web.xml? The mapped URL (WebAppName/*.jsp) or the real one (WebAppName/pages/*.jsp)?
Thanks in advance.
You could make a servlet - or JSP - that has a mapping *.xhtml (other extension). Using the request URI it can do a dynamic include of the corresponding JSP from pages.
In servlet:
String pagesPath = "pages/" + ...;
request.getServletContext(pagesPath).getRequestDispatcher().include(request,
response);
I doubt it is a good idea, as it does not add something: you might even need to adapt all JSPs (other extension). Or rename the JSPs to .jspf.
In general I use JSPs under WEB-INF/jsp or so; so they are not publicly accessible. And use a similar technique: creating a model in the servlet, and then to the view as JSP.

Download file from physical path in java

I have uploaded files using spring MVC file upload functionality and files are getting uploaded in /home/myfolder/images/ folder. Now I want to download these file from this physical path. For test in my jsp I have written the following line
<a href="<%=request.getSession().getServletContext().getRealPath("/home/images/image.jpg")%>" >download </a>
but when I click on this link it redirects me to the URL
http://localhost:8080/home/myfolder/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myproject/home/images/image.jpg.
How can I download the image saved. Please let me know if there is anything else you need from me to resolve this.
Try using this:
<!-- Handles HTTP GET requests for /resources/ ** by efficiently serving up static resources -->
<mvc:resources mapping="/images/**" location="file:/home/images"/>
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
<mvc:default-servlet-handler/>
In the jsp:
<img src="/images/image.jpg" />
Here is the thread: Spring : serving static resources outside context root
Files outside your Webapps folder can't be served by the application container.
Some possibilities:
1) Upload to a folder that is below your Webapps folder in your application container. E. g.
Given your Webapps folder is /home/myfolder/Tomcat/webapps/myApp/
You could upload to /home/myfolder/Tomcate/webapps/myApp/upload/
This way the uploaded files can be served by the application container.
2) Make the upload folder accessible to the application container by means of symbolic links. You might need to change your container's config in order to make this work.
3) If you use a webserver in front of your application container (like httpd) let the webserver serve the static files.
4) Write your own servlet that reads from an arbitrary file and serves the contents to the client.

Servlet container : how to forbid access to a folder

I use Jersey for my REST API. I use JSP files for the views. I put my JSP view files in www/views/.... Now I'd like to forbid access to these views through simple HTTP request on their canonical URL.
What is the best way to forbid direct access to these JSPs from the client ?
Stick the JSPs under /WEB-INF in the WAR.
As johna has already said, if you put the .jsp files under WEB-INF this will prevent any access to them from the web directly.
If you want finer-grained security controls I would investigate a tool like Spring Security which will let you set up more complex security rules.

Categories

Resources