Using HTML,JS and CSS in spring mvc application - java

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.

Related

Any way to convert my JSP project into Spring, though it has so many JSP files?

I have a project which is coded in JSP. It has about 60 jsp files. Now i want to change the code from jsp to spring. So is there any way to do this.
Should i write each jsp file in spring individually or any short cut for this transformation .
How to start please give any suggestion.
Note: There is DATABASE connectivity also.
Thanks
you can add spring dependencies in your project and can follow MVC pattern where your (business logic) should move to Controllers and database related code (Connection to DB, Query to DB) should move into Services and you can use your JSP files for views only.
My suggestion is follow Spring model view controller design pattern. some points
1.Add dispatcher servlet in your web.xml file
2.Create spring mvc controllers for each jsp page request.
3.Create service layer for businness logic and transaction management.
4.Create DAO layer for database operations like save,update,delete,getbyId,getAll entites.
5.Follow spring mvc reference doc for more configuration and application context.
6.Study how MVC works and you can use URLRewrite filter for url's security.
Apart from this for database persistant layer my suggestion is to use Hibernate Framework.

Spring Boot - Proper location for html files

I am new to Spring Boot and I am trying to add a simple html page for my project
At the moment, my project's structure is the following:
Having read the following:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
and other answers in StackOverflow, I'd expect to see the contents of index.html when I visit http://localhost:8080/
What could I be missing?
As there is no webapp in SpringBoot project , we can place the html files in either in
src/main/resources/resources/index.html
src/main/resources/static/index.html
src/main/resources/public/index.html
Remember the above is in highest to lowest precedence.
To check your file run the main class in eclipse and go to http://localhost:8080/index.html
First of all the correct way to serve files in Spring Boot is described in
cannot find html pages with spring boot
After adding the directories required, I tried /services/login.html where "services" is the mapping for my Dispatcher servlet.
So what happens here is that Spring Boot will only answer to requests sent to /services/* even if these requests are pointing to something other than a controller.
I guess that this makes sense in the world of Spring Boot but when I tried in the same in a Spring MVC project (no Spring Boot) I was able to access simple html files that I had simply added into my WEB-INF directory

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.

include css inside a spring mvc java project

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.

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