Navigating in spring URL - java

I have a question regarding navigation.
Currently this is my URL
http://localhost:8080/spring/Account/ump
I would like to navigate to
**http://localhost:8080/spring/Scheduler/test**
this is the URL i am trying to hit but i cannot find the correct ahref code to achieve
My Code
<li>test page</li>
I have tried alternatives such as
<li>test page</li>
The result would be
http://localhost:8080/Scheduler/test
and for the redirect result it will be
redirect:/Scheduler/test
I use a InternalResourceViewResolver if it matters.

If you are able to use JSTL on your pages, use the c:url tag which is defined in the core JSTL. The url tag always appends the actual context path to the start of the url.
example:
<li>
test page
</li>

Related

Is there a way to display thymeleaf tags on an angular JS partial?

Currently, I have my main page rendered by the thymeleaf resolver server-side, so tags such as
<span th:text="#{userId}"></span>
works properly. However, when I load a partial page using angular's ng-route and routeProvider like
$routeProvider.
when('/c1000', {
templateUrl: 'pages/C1000.html'
});
and in my C1000.html, I have a tag such as
<span th:text="#{userId}"></span>
It does not display. I am assuming it's because the main page is resolved by thymeleaf's resolver while the partial is loaded as an ordinary HTML file. I have implemented internationalization so the text for #{userId} changes language based on the properties file. What I want is for the partial page to also display #{userId} similar to how my main page displays it. Is there a way to do this?

forward from spring jsp to spring jsp

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

How to print list of absolute URLs in JSP

I want to print a list of absolute URLs in a Spring JSP, for an internal user to pick from. However, the page is rendered with the current URL prepended.
For Example I want to a link to www.anothersite.com, but the links comes out as http://localhost:8080/myapp/www.anothersite.com on the page
What am I doing wrong? Both lines below have the same result.
<c:forEach items="${listAppURLForm}" var="nextURL">
<li>
</c:out>>${nextURL.link}
<a href=${nextURL.link}>${nextURL.link}</a>
</li>
</c:forEach>
There's a misconception. An URL like www.example.com is definitely not an absolute URL. The URI scheme is completely missing. You need to prepend the URL with the desired scheme to make it really an absolute URL, like so http://www.example.com.
If you can't edit the URLs directly in the list, then you'd need to prefix it in HTML instead.
${nextURL.link}
You might want to perform a ${fn:startsWith()} check beforehand to prevent duplicate schemes.

How to call a method which populates bean, When I press a anchor tag with help of c:url

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.

Spring MVC: Relative URL problems

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">

Categories

Resources