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>
Related
Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?
add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.
I have two jsp, let's say A.jsp and B.jsp. A.jsp has the following code:
<c:import url="B.jsp" >
<c:param name = "page_title" value = "Title" />
</c:import>
In B.jsp i need to check some conditions and do a redirection to Login.jsp. I achieve this by doing response.sendRedirect(Logn.jsp)
The problem is that the redirection is made on B.jsp so the result is that the browser displays the content of Login.jsp and A.jsp but i need to redirect the whole page to Login.jsp. That is, the browser should only show Login.jsp.
Consider that the redirection must be made on B.jsp unless there is a way that B.jsp can tell A.jsp the url to redirect to.
EDIT: A.jsp and B.jsp belong to different projects
In your B.jsp, instead of doing a redirect, set a flag on the request as
<c:set var="login" value="true" scope="request" />
Then in A.jsp check the flag and redirect if present.
<c:if test="${login == 'true'}">
<c:redirect url="/login.jsp" />
</c:if>
Try doing a jsp:include instead.
<jsp:include page="B.jsp" >
<jsp:param name="page_title" value="Title" />
</jsp:include>
There are two parts to solving this:
1) Making sure that you are not sending any HTML to the client in A until you have performed the logic in B. That means that you must call the B.jsp early (part of good MVC design).
2) Use the var parameter in your c:import:
<c:import url="B.jsp" var="output">
Now the output of the call to B will be in a buffer called "output" instead of being sent directly to the client. That allows you to make decisions in A (after the call to B). If you decide you want to redirect, you can, because you have dumped nothing (except some white space probably) to the browser. If you decide you want to send the output of B to the browser instead you can simply do this after the c:import:
${output}
There is even a trick to get rid of the white space being sent to the browser if that becomes necessary, but it rarely is (if you send too much white space the buffer will commit and then you cannot redirect, but that's rare and an entirely different question).
Make sense?
in catalog
webapp/WEB-INF/views located my jsp pages
I want to forward from 1.jsp to 2.jsp
in 1.jsp I write
${candidate.name}
but it doesn't work.
How to fix it?
Use <c:url and then the value attribute of <c:url is set to the href link to the other JSP. So for example, it would be:
${candidate.name}
I had used it in the following manner in my webapps (where circuits is a folder under /WEB-INF/views):
Edit Circuit
A relative url should start with a / character
Change the anchor tag as below and try again
${candidate.name}
Reference Link
What is the best way of obtaining context-root on a jsp page. If I hardcode my css/image to "/myapp/images/foo.gif" then it will be a broken link if the context root / app name is changed. Using relative path is not always preferrable because a context root can be multi-path (eg: /mysite/myapp)
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/), however if this is the very first time user is visiting the site (or if cookie is cleaned on the browser), the value assigned to ${root} became something like /myapp/;jsessionid=019762g1hk3781kh98uihilho and it breaks the images/css reference. Is there any other better way than this?
So far I've tried using <c:url var="root" value="/"/> which works alright (${root} will give the context-root /myapp/)
This is not the right way. The <c:url> should be applied on every single URL individually.
You'd better use
<c:set var="root" value="${pageContext.request.contextPath}" />
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
I want to call my web site like:
http://localhost:8080/?co=grav
When doing this the authentication page configured in web.xml comes up and the login is done through form login. After that it will go to index.jsp where I am doing a redirect to one of my main pages of the site:
<c:redirect url="index-userapp.jsp" >
The problem is that, this redirect does no longer forward my co parameter, and I really need to not loose it...
Do you see a workaround?
I think it can be done like:
<c:redirect url="index-userapp.jsp">
<c:param
name="co"
value=""></c:param>
</c:redirect>
but how I put in the new value, the old parameter value from the initial request?
try this:
<c:redirect url="index-userapp.jsp">
<c:param name="co"
value="${param.co}"></c:param>
</c:redirect>