User Downloads In Java Spring - java

I need to make a link in my Java Spring application that will allow the user to download a CSV file . I have the CSV file in my project's root directory and I have an tag in my view that runs a Javascript function when it's clicked. Is there any way I can use either Javascript or the Java Spring framework to allow the user to download the CSV file? I'm a noob Spring user/developer so any help is appreciated. :)

Put the file in your WebContent (or whatever you call the web resources directory) and link it directly. Everything below WebContent (but not in WEB-INF) is accessible in your context root.
So file WebContent/test.csv is accesible with relative url /test.csv.

Related

Java web project - how to serve html file sitting on desktop with images

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.

How to upload HTML directory to spring server and then create link to view the index.html file in that directory

I'm trying to upload a number of website directories to a server with spring, and generate links so i can view them later on. I realized i can't put web directories right into the standard resources folder because that's inside the web archive, and if i try to use an external resources folder it just gets shuffled into the web archive when the app is built. My other possible approach would be to make a separate downloads servlet, but that seems like it wouldn't work because if it only downloads the index.html file from the site, it wouldn't also show the other files that the site depends on, which is not what i want.
I understand that relying on system paths is not very portable but probably this program will only run on at most 3 or 4 linux computers so I could just set up the file path on them manually if i can't just do it from within the java program itself.

How to upload a folder on a web server using Spring?

I am using the Spring MVC framework to create a website. One of the features that I will need to have is the ability to upload a folder.
However, by only using Spring, I can only upload a single file or multiple files. Alternatively, I can ask the user to create a ZIP of the folder to upload, but this would be their responsibility to do that.
Is there a way to, within HTML, select a particular folder? Is there any way to copy that entire folder to a server?
you can use spring multifile feature to upload multiple file.
please refer given link , may help you to solve your problem.
http://howtodoinjava.com/spring/spring-mvc/spring-mvc-multi-file-upload-example/

How to add external resources to dynamic web project eclipse?

I'm creating a dynamic web project in eclipse using jsps and java servlets, however I want to add some external files to be edited using the app. Where do I put them such that I can open them from my app and save an edited version - and finally provide a link for a download of the edited file?
Thanks
Where do I put them
Nobody cares. Really. As long as it's not in the deploy folder, of course.
If your concrete problem is avoiding to hardcode the exact external location in Java source code, just provide it as VM argument, environment variable, properties file setting, or whatever externally configurable. For detail, see also Recommended way to save uploaded files in a servlet application.
And/or if your concrete problem is serving those files back to the web, just either tell the server to publish the external location into the web as well, or create a servlet which reads from the external location and writes to the response. For detail, see also Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag.

Spring MVC and Angularjs

Currently I´m trying to learn Angular JS, but firstly I want to setup my environment with Spring mvc.
At the moment I only want to work with rest, but I have a doubt for what is the best way to place the resoucres in Spring MVC
My simple applicaction has this squeleton:
my-simple-app:
src
main
java
resources
webapp
resources
WEB-INF
If I want to put the app folder from the angular-seed, what is the best place to put it?
I tried to put in src/webapp/resources/app but then I have to move the html files to WEB-INF?
How was your skeleton in your angular-js spring mvc applications?
What is the best way to do the redirect to the app/index.html? to the welcome file and then work only with angularjs $routeproviders?
Thanks!
As far as I can understand, your Front End technology is Angualar JS and your Back End technology is Spring MVC.
I'm a Front End developer and hence I can provide you the advice on the structure of your HTML, CSS, and Javascript.
Here are my recommendations:
Mode Of Communication Between Front End and Back End: JSON (Should be strictly followed for MVC Pattern)
File Location: All your Front End files should be in WEB-INF folder with this structure:
WEB-INF/Assets: All your JavaScript Files, JavaScript Libraries, Images, CSS etc. Should Be Places Here. You Can Open A Separate Folder Each Resource Type Inside Assets
WEB-INF/JSP: All Your JSPs should be placed here. As Angular's greatest strength is Single page Application, you can create one JSP per main page and place them here
WEB-INF/HTML: All the static resources that would be injected into the JSPs using <ng-view>or <ng-include> can be placed here
Hope this helps!
The easiest way to get going is to take all the files in the angular-seed/app directory and copy them into your src/main/webapp directory. After copying these files, you should be able to redeploy the app and have a running sample.
Most servlet containers will include index.html as a default welcome file. If yours doesn't, you can add that config in web.xml <welcome-file-list>.
The WEB-INF directory is for web resources that should not be exposed directly to the web. web.xml is a example of a file that should not be exposed to remote users. In this case, it's safe to expose all of the app's resources directly to the web; thus you don't need to place the resources under WEB-INF.
I recommend this structure for your project:
my-simple-app:
src
main
java
controller
MySpringCtr.java
models
Person.java
House.java
*.java
webapp
WEB-INF
resources
css
style.css
*.css
js
angularCtr.js
*.js
pages
index.jsp
*.jsp
mvc-dispatcher-servlet.xml
web.xml
I found a tutorial for beginners which explains step by step how to combine SpringMVC and AngularJS, you can find the tutorial and the complete code in this blog I hope it will be useful :)

Categories

Resources