My root URL keep messing up in the Spring MVC application. I have the following resource defined in servlet-context.xml
<!-- Resource Mapping -->
<resources mapping="/theme1/**" location="/resources/theme1/" cache-period="31556926"/>
On the header of localhost:8080/me/login.jsp I have the following CSS file
<link rel="stylesheet" href="theme1/css/main.css" type="text/css"></link>
Now on the same JSP, I have an anchor text register which maps to /useraccount/register.T
The method in my controller returns the same view, login.jsp. So after a page refresh, the domain is changed to localhost:3000/me/useraccount/register
So the problem is that style sheet URL becomes invalid - return 404. It now points to
localhost:8080/me/useraccount/theme1/css/main.css
How do can I fix this so it points back to - in a sense being static - to
localhost:8080/me/theme1/css/main.css
Related
I have a spring mvc application, in a page I list the "Group" details in a table, fetched from database. The url of every group is set to "/viewgroup/410", where 410 is the groupid which will be loaded from the database and displayed in the viewgroup.jsp page.
<td>${group.name}</td>
So, the controller method has the
#RequestMapping("/viewgroup/{groupid}")
public String viewGroup() {
...
}
like this. The jsp pages could not load the static resources which I have in the directory structure
+webapp
+resources
+css
+js
+images
+views
+login.jsp
The jsp pages has the below path for image/js/css.
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
I tried adding
#Controller
#RequestMapping("/viewgroup/**")
public class ViewGroupController {
...
}
and this in jsp
<link href="viewgroup/resources/css/bootstrap.css" rel="stylesheet" media="screen">
Still the jsp page loads could not load the static resources. How do I provide the resource path of the static resources in jsp pages when I pass params in the url?
The fact that you pass query parameters through any URL of your project does not affect the way it addresses static resources. You need just to include a link to your static resources (as a relative path for example) as follow :
<link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">
The resolution of such static resources is completely independent of the page you're currently looking at, be it /viewgroup/410 or /foo/bar. That's the reason why you don't need the "viewgroup" at the beginning of your link's href.
But you do need to tell Spring MVC how it should address such static resources. Usually, this is done in the Spring configuration. For example, if you use Spring MVC java config through an WebMvcConfigurer instance, you should override the addResourceHandlers method in such way :
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
EDIT : My bad, I thought Spring MVC + any view technology (JSP, Thymeleaf, etc etc) would automatically resolve link's href against the root path of the web-app but this is obviously not true to raw HTML which relative link's href are resolved against current path.
So in the end, as found by OP, links are only to be resolved against root path if they are processed with the view technology in use (in the example with JSP : <c:url value="/resources/css/bootstrap.css"/>)
The style sheet does not apply on my first JSF page. I've got a index.jsp which forwards to my first JSF page.
<html>
<head></head>
<body>
<jsp:forward page="./start.jsf" />
</body>
</html>
On start.jsf the style sheet does not apply but if I navigate to a second page my style sheet fully applies.
The second page was my first page before and I've had the same behaviour. Without changes, the second page works fine as long as the page is not the first one in row.
Therefore CSS and the page itself must be correct. I think it's a configuration issue.
Any ideas?
You should invoke the index page using an URL which invokes the FacesServlet. It's namely the one responsible for doing the JSF works. So you need to invoke it by index.jsf instead of index.jsp.
However, better is to get rid of this hacky index page altogether and define start.jsf as <welcome-file> in web.xml instead.
<welcome-file-list>
<welcome-file>start.jsf</welcome-file>
</welcome-file-list>
and supply an empty start.jsf file in the same folder next to the start.jsp file so that the servletcontainer will be fooled that the index page really exists (it namely by default doesn't check on any servlet mappings for the index page).
Try to use redirect instead of forward. You can do this like this in your jsp:
<% response.setStatus(301);
response.setHeader("Location", "/start.jsf?" + request.getQueryString());
response.setHeader("Connection", "close");
%>
or use
response.sendRedirect("/start.jsf?" + request.getQueryString());
It is not best way to solve problem, I use in my project tuckey urlrewrite:
<urlrewrite>
...
<rule enabled="true">
<from>^/$</from>
<to last="true">/index.jsf</to>
</rule>
...
</urlrewrite>
I have a JSP page (home page) in a Web App, which has different anchor tags (like home, video, pictures, profile etc) for navigation in a Web App.
For, anchor tags I am using JSTL tag (c:url)
The Process:
I am on home page and press a anchor tag (like profile).
Anchor tag, which uses c:url tag of JSTL redirects it to profile page.
The profile page gets displayed with user information
The code that I am using in JSP:
<a href="<c:url value="/profile.jsp" />" >Profile</a>
What I want?
When anchor tag (profile page) is pressed on the home page, a method should be
called to fetch the user details from database and populate it in appropriate
bean So that it can be accessed in that profile page using Expression Language
Is the above question/or method right to do so? If no, then Which process is
better?
So the method you describe is a good start. However, to do the database part it might be better to look at using a Servlet. So you create a Servlet class that implements the doGet() method.
You have to create a mapping for your servlet in the web.xml file, so assume that you map the url: /profile to the servlet that you create then the link will be:
<a href="<c:url value="/profile" />" >Profile</a>
In that method you interact with your database, populate your bean and save it in the request scope. Next, you forward to the jsp page that will display the bean data.
On the jsp page the bean will be available for you to display the data.
I have a controller bound the URL: "/ruleManagement".
Inside my JSP, I have a form that forwards (on submit) to "ruleManagement/save" url. When there are errors with the input fields, I want it to return back the original form View. This is where the problem starts...
Problem 1) Now that the URL is "/ruleManagement/save", my form submit now points to "/ruleManagement/ruleManagement/save".
Problem 2) I tried using spring:url tag to generate the absolute paths for me, which usually works great. But when I put a spring:url tag inside of a tag, the spring:url tag does not get parsed correctly.
<form:form action="<spring:url value='/ruleManagement/save' ...>" method="post">
When I analyze the DOM after the page loads, my form tag looks something like:
<form action='<spring:url value="/ruleManagement/save" />' ... >
If I don't use the spring:url tag, and instead use just "/ruleManagement/save", the url generated excludes my application name in the url, which is also wrong.
How do I generate a consistent URL pattern across all Views regardless of path? If the answer is "using spring:url", how do I get that content inside a form:form tag?
Custom tags in JSP can't be used in attributes of other custom tags, so you need to store intermediate result in a request attribute (using var to redirect output of the tag to the request attribute is a common idiom supported by many tags):
<spring:url var = "action" value='/ruleManagement/save' ... />
<form:form action="${action}" method="post">
I too would love to be able to generate a consistent URL path across all Views! Is this possible with <spring:url .../>.
To answer your second question & tacking on to axtavt's answer, embed the <spring:url ... /> into the form action after adding the property htmlEscape="true"
Example: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">
From a servlet, I'm forwarding the request to a JSP page which renders a FusionChart.
But I've a problem in loading the chart. The JSP file is not detecting the JavaScript file. The folder structure is:
axis
|
WebContent
|
WEB-INF
|
classes
|_ com
|_FusionCharts.js
|_MyChartJsp.jsp
|_Line.swf
And the JSP code:
<html>
<head>
<script language="text/javascript" src="/WEB-INF/classes/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="left">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">
var foo = //value fetched from DAO
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
"myChartId", "1000", "500");
myChart
.setDataXML("<graph caption='aCaption' xAxisName='xAxis' yAxisName='yAxis' showNames='1' decimalPrecision='0' formatNumberScale='0'>"+foo+"</graph>");
myChart.render("chartdiv");
</script>
</body>
</html>
The Servlet code to forward the request:
final RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/classes/MyChartJsp.jsp");
requestDispatcher.forward(request, response);
The request is getting forwarded to the JSP. But the chart is not getting displayed because it is unable to figure out what FusionCharts is in the line
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
"myChartId", "1000", "500");
I tried
src="/FusionCharts.js"
src="FusionCharts.js"
but no luck.
Has it something to do with the request being forwarded??
You cannot have .js (or .swf, .jpg, etc.) files in WEB-INF - they are not publically accessible.
Move it to /js/
There is no reason to hide static resources (like scripts and css) in WEB-INF. If you insist on that, you should make a servlet that, given the name of the js/css, would read it from its location and will serve it as a response. This is what the default servlet does when you access static resources.
The flow of the page loading is as follows: the browser sends a request to the servlet; the servlet forwards internally to the JSP, and the JSP is rendered as a response; then the browser parses the <script> tag and fires another request to the script. If the script is not accessible via URL, it's not loaded.
Then, to make the script url fixed to the servlet context root, use
src="<c:url value="/js/script.js" />"
This will work regardless of what is the current url
Not the cause of your problem, but also note that your <script> element is incorrect. It should be <script type="text/javascript"....
(I tried to post this as a comment, but for some reason it wouldn't let me.)
I was facing same issue. In my case when I calling the myFile.jsp directly its reading the myFile.js;
But when calling through login-> myFile.jsp, its not reading the myFile.js;
After analyzing the path through the Developer tools :=> console, I found that its inserting the uri, so final path was incorrect.
Final Solution:
I'd used the absolute path for all .js and .css. Now its called from everywhere.
My Project Structure is:
In my servlet-context.xml
i) <context:component-scan base-package="com.SBP.SHWeb" />
ii) <resources mapping="/resources/**" location="/resources/" />
My old path for including .js was: /resources/MyJs/myfile.js ===> Its not get called sometimes.
My Absolute path, which get called from all places is like this:
/SHweb/resources/MyJs/myfile.js ==> Its get called from everywhere.
Hope it help you.