how to modify html in a struts...links to jsp - java

I am new to struts and I am not sure of jsp either. I am a php scripter with JS and C# experience and various other language including java but not the struts or jsp component.
I am confused as to how I can modify the html of a struts file once I have found the config in xml and the jsp linking to it.

JSPs are typically where you would look for HTML. So, you can usually just open the JSP and edit the HTML.
However, if the HTML is being generated dynamically, you may not see it there. There should be a Tag or Java Scriptlet that should give you some indication of where the HTML is coming from, though.

Related

JSP page included in servlet not rendering satisfactorily

I have two jsp files (header.jspf and footer.jspf) that I want to include in a page produced by the doGet method of a servlet (Menu.java). I'm doing this by use of the RequestDispatcher.include() method. The header.jspf file only contains html so it renders fine. The footer.jspf file, however, contains both EL and JSP tags, neither of which render on the servlet page produced by Menu.java.
Looking at the source code of the page produced by Menu.java I understand that the reason for this problem is that the EL and JSP is not evaluated prior to being included in the servlet so it is just presumed by the browser that it is html.
While I'm guessing that what I'm trying to do may be poor (and deprecated) practice, I'd just like to find out if there is a way to get an included jsp file to render in a servlet just as it would in a jsp page when it contains EL and jsp tags?
I'm thinking that this question is general enough not to require my code to be posted, however if I'm wrong please tell me so and I will update with my code.
Not sure if there is a way to have RequestDispatcher.include() files processed, but including the footer explicitly will very likely work:
<%#include file="footer.jspf" %>

If I have html elements in JSP then what is order of execution?

What will execute first body elements or head elements...
Head
Body
scriplet
If I get what you're asking, each element in the JSP file is processed in the same order as it appears from top to bottom of your code.
Obviously, the scriptlets embedded in your JSP are executed to create the HTML. The scriptlets, and other server-side-executable stuff in the JSP is executed top to bottom.
(The JSP is not aware of the HTML elements it is generating. It processes the JSP / JSTL syntax embedded in the JSP file, and treats the rest as text to be copied into the document sent to the browser. That's why you can, in theory, use JSPs to generate any text-based document.)
The generated HTML is then sent to the browser ... which is where any client-side javascript embedded in the HTML will be executed.
JSPs are servlets that add syntactic sugar to facilitate developers. All JSP compile to servlet first and at runtime class files of that compiled JSP.
Following diagram explains the JSP compilation/execution in detail:
HTML/JS are executed on browser(client side) once server side script finalize the response as HTML.

Syntax Error in my JSP Eclipse

Why I have this error in my JSP file
My JSP:
Error Message:
Syntax error, insert "}" to complete MethodBody
A JSP has been parsed incorrectly by Eclipse. It has mistreated curly braces used by some javascript code that was rendered without <script> tag.
The <script> tag should be placed inside the <head> or <body> tags.
JSP files are compiled to a servlet. If you declare a method inside the JSP page using scriptlets, the method body is opened with { and should be closed with }, but somwhere in the code you might find /*}*/, or even worse missing <%}%>. The example of such errors (redundant }; in the Java code) you can find here.
To resolve this and other possible errors caused by spaghetti code inside the JSP, recommended way is to not use scriptlets and move Java code to a servlet. Struts is MVC framework that provided additional to servlets features for rendering JSP pages. You have to move Java code from JSP page to Struts controllers and access it by expression languages like JSTL, OGNL, etc. Return JSP page as a result/forward of the action/controller execution/ method call. You can also call methods of the model/controller directly while the page is rendered. This behaviour is out of the scope in which MVC pattern is used.
If you need more information about separation of concerns while developing a web application, particularly related to how to avoid Java code in JSP see How to avoid Java code in JSP files.

Access Spring MVC Model data in imported js file

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.

Does Spring MVC 3.x support partial views (html/jsp) like that of ASP.NET MVC3/4?

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.

Categories

Resources