I am working on a project with Spring MVC with java as the backend and HTML(JSP)/CSS/JS as the frontend.
In my controller in java I am adding data to the model like this
model.addAttribute("someData", theData);
which I can easily access in the JSP file like this
${theData} or "${theData}"
The problem for me is that this only works in my JSP file. I am importing other css and js files there like
<script src="<c:url value='/resources/javascript/main.js'/>"></script>
and they seem to have no access to the model. I understand that there is no model at runtime on the clientside, but the javascript files are served by the server the same way as the HTML/JSP file, so why does this not work? Or is there any kind of setting I have to use?
I realize that inline js code in my JSP file works, but this is not very flexible and just not a "real" solution for me. Also I would like to avoid additional calls to the server (like AJAX) to get my data.
Thanks for your help!
...but the javascript files are served by the server the same way as the HTML/JSP file...
No, this is not true. The static JavaScript, CSS, and HTML files are being served by the web server as-is, with no processing. The EL snippet in your example...
<script src="<c:url value='/resources/javascript/main.js'/>"></script>
...is only generating a concrete URL based upon the relative URL that you have provided as an argument to the function. There is no knowledge embedded of what main.js is or does. Your JSP files are explicitly processed by the Java servlet container. This is why it has knowledge about your application, beans, models, and all. This is a gross oversimplification, but hopefully it gets the point across.
Edit
You can use JSON from static files in your JSP, but you have to read, process, and store them as Java objects in a bean in your application. I am not sure why you would want to do this, though, since it is so easy to read JSON data via JavaScript.
willOEM gave you a good background. So static files don't know about the model values, but also there's no need for this as you can always code your way out.
If you're calling any function from your main.js within your jsp, than at that point you should use the ${theData} as a function argument. If not, you can always use a data attribute on a dom element, store your model variable there, and access it inside your js file, so something like
<div data-the-data="${theData}">Lorem ipsum</div>
I have "solved" it now by defining the databinding inline in the html document
var theData = "{theData}";
and then using that in my standard javascript file.
Not really perfect but seems to be the best working solution.
Related
Does anyone know if it's possible to process a JSP page from within Java code and get the resulting HTML string?
I'm working on a project that has a Spring Boot application. One of the requirements is to have an end point that returns JSON. The JSON is suppose to contain one or more properties that contain HTML strings that were generated by JSP templates. The JSON will contain other other properties as well.
I've done some research and I've found examples showing how to process Freemarker and Thymeleaf templates from within Java code. However, I've been unable to find any examples showing how to do this with JSP. Ideally we would like to use JSP as that's what our developers know and in the interest of time there would be no learning curve as opposed to if we went with Freemarker or Thymeleaf.
Has anyone out there done this before with JSP?
I try to use properties file to manage the string text.
For now , the java's property file is located at src/org/XXX/lang/bundle, and the jQuery.i18's is located at WebContent/i18n.
Is there a way to share the same property file ?
It is possible to share the same properties file. One way suggested in a comment was to simply copy the file over; it could be easily done by the build script. Frankly it seem the easiest solution.
Other ways to share the file:
A servlet or a web service that will return the required content on HTTP GET request (i.e. dynamically generate required language file) - requires some coding,
You can use the same method you are using to translate the html templates (whatever technology you're using, JSP, Thymeleaf?) to either generate an array to be used by jQuery or create pre-translated HTML pieces (i.e. Handlebars templates).
The choice depends on the technology stack as well as requirements (i.e. response time); definitely it will be faster if you already have pre-translated HTML parts that you can reuse, generating HTML from strings always brings overhead.
Q1: Does Spring or any opensource java UI framework support partial views like that in asp.net mvc?
For example in my main index.html (or _layout.cshtm per asp.net mvc3 spec)
I would have the folllowing code:
<span id="logindisplay">#Html.Partial("_LogOnPartial")</span>
where #Html is a helper to display a partial view for _LogonPartial.cshtml which just injected it's html view contents into the page?
Q2: If this is supposed If I want to display a bunch of partial views it would be helpful to display them concurrently in parallel to improve performance. Very similar to what linkedin is doing using dust and fizzy? http://engineering.linkedin.com/profile/engineering-new-linkedin-profile
Q3: Is fizzy available as open source like that of dust?
If you want to include content of a page into another page, by adding some code on the page itself, you should compare asp with jsp, not ASP.NET MVC* with JEE - Spring MVC
So, an equivalent to <span id="logindisplay">#Html.Partial("_LogOnPartial")</span> on a jsp would be one / all of the following
On your jsp, include content from another jsp using <%# include file="../includes/inner-content.jsp" %>. This is what is called a static include. The source of the included jsp is included and made part of the parent jsp, before the jsp is compiled. If you use an IDE, it will check to ensure the included jsp does infact exist at the path specified, relative to the location of the jsp in which you are adding the include. Technically this is a JSP Directive. The jsp being included could just be a fragment, and not addressable from the outside world (could be hidden inside WEB-INF)
You can also use what's called a Dynamic include <jsp:include page="someJSP.jsp" />. In this case, the included JSP should be addressable from the browser and should be capable of being rendered independently. When the server is executing the servlet to render the parent JSP, it stops when this tag is seen, and starts executing the servlet for the included jsp, the output obtained from the inner jsp execution is then merged to the output of the parent jsp, and processing of the parent jsp is resumed.
A third option would be to use Core JSTL taglib's <c:import url=""/>. This works just like option 2 above, except it also allows you to import a page / content from a url that lives outside your application. Basically you can mention a path to a jsp, or relative URI to a servlet mapping in your application, or a URL to an external page.
Now, I suspect this is not really what you want to do, if you are comparing with what Linkedin is doing. You want to mashup content from sources in your own application, and compose your page. You also want to do this in an asynch manner so as to keep load times in check. In this case, you HAVE to use JavaScript and Ajax. All the mechanisms described above are for server rendered pages (All HTML is created before the page is rendered in the browser). Just like #HTML. You need to create a simple framework / use an existing one, where once a page loads, it fires asynch ajax calls to the server to get content for specific areas on the page and renders the returned HTML in those specific areas.
Hope this helps.
Do let me know if I've misunderstood your question.
I've been tasked with building a new Javascript file browser but unfortunately I have to use Prototype and can't use Jquery (at least not at the moment) due to some issues that may arise from conflicts (they have said they plan to resolve this but for the moment it looks like I may have to go with prototype). I've been searching on google for plugins that I can use for Prototype / Scriptaculous but they seem few and far between and all the ones I have found are geared towards use with PHP. I figure I may be able to rewrite the PHP end to Java / JSP as our application is built with Java. I just figured I would post here to see if anyone can recommend a good plugin for this that could easily be made to integrate with Java. Thanks
EDIT: This is what I mean:
http://abeautifulsite.net/blog/2008/03/jquery-file-tree/ (this is in Jquery) so I need something like this for Prototype
This is for browsing the file system of the server, not the client, so there would be a server-side page / controller that returns an HTML list, of sorts, of the files / folders and then the JavaScript manipulates this as needed to collapse / expand. There are a bunch of these written in Jquery but unfortunately I can't seem to find one written with Prototype
As pointed out by Diodeus, and as demonstrated by your own example, and as probably used by most file browser plugins available it works on HTML being rendered and returned by the server. All the javascript needs to do is insert the returned HTML, Prototype has a function which does most of that for you, Ajax.Updater. You could write your own very quickly.
function expandCollapseFolder(event, folder) {
var list = folder.down('ul');
if (list) list.toggle();
else new Ajax.Updater(folder, 'URL-OF-SERVER-PAGE',
{
parameters: { path: folder.readAttribute('data-path') },
insertion: 'bottom'
});
}
Event.on('ID-OF-FILE-BROWSER', 'click', 'li.folder', expandCollapseFolder);
The above is not tested code, it is meant to show how little is needed in total. The hard work is done by the server, it needs to scan and return folder contents with the appropriate file icons and URLs.
<ul>
<li class="folder" data-path="/folder">
<img src="folder-icon.png" />This is a folder
</li>
<li>
<a href="URL-OF-FILE" target="_blank">
<img src="file-icon.png" />This is a file
</a>
</li>
</ul>
I've been searching on google for plugins that I can use for Prototype
/ Scriptaculous but they seem few and far between and all the ones I
have found are geared towards use with PHP.
Based on your question I'm not sure exactly what you need to accomplish.
Prototype & Scriptaculous are add-on libraries for JavaScript, just as jQuery is. They have absolutely nothing to do with PHP, Java or any other server-side language because they are part of the client side: JavaScript.
If you're making one to browse files on the server, you're simply sending a HTML representations of the file listing to the browser. You would use Prototype to manipulate this HTML.
There are no Javascript-based client-side file browsers, regardless of whether you're using jQuery or Prototype because JavaScript cannot see the local filesystem on the client machine. This was deliberately designed this for security reasons.
I am working on a Mail API module where i have to develop a generic functionality of sending Mails with respect to various functionality in the Appliication.
There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template.
It expects that this will extract the content of the JSP which will be a well formated mail template and send mail.
Is there any way in JAVA where i can extract the content (HTML) from this JSP page so that i can use that HTML content in to the Mail.
Thanks in advance
You have two paths to go, with the first one being a little shorter:
use new URL("http://site.com/url/to/page.jsp").openConnection(), get the InputStream and read the contents - this will be as if your server sends a request to itself and gets the result
use a Filter and a HttpServletResponseWrapper, and return a custom Writer / OutputStream. Each time something is written to the writer / stream, delegate it to the original object, and also write it somewhere where you can read it from later. This explanation is not sufficient, because this is less likely what you need, but if you are willing to take this path, tell me.
That's, however, not the way this is usually done. You'd better use some templating technology like Freemaker or Velocity for your email templates.
It sounds like you're trying to use JSPs as a templating engine for your email, which is something it wasn't intended to do. There are other technologies out there better suited for what you want, like Velocity and Freemarker.
However, if you're dead-set on using JSP, you have two options :
1) You can use the method described by Bozho to, essentially, connect to your own site and have it generate the content for you
2) You can write the JSP, compile it at compile time, and include the generated servlet file in your email generator and mock the inputs to the Servlet API that the generated JSP servlet will be expecting to extract content from your compiled JSP.