how to avoid <spring:url />ing multiple times - spring

I'm working with <spring:url /> because our project is located at domain/ProjectName/mappings/here. I posted about some confusion here: How to use <spring:url /> with an <a> tag? . Now the issue I'm having is because I'm saving the URLS to a database. So I get URLs like domain/ProjectName/ProjectName/mappings/here. How can I avoid this? If spring needs to add ProjectName, shouldn't it be able to check for it's existence first?

From your last comment, yes the spring:url tag does have an option to exclude the contextPath portion of the URL. Just set the context attribute to an empty string:
<spring:url var="mappingLink" context="" value="/mappings/here" />
I'm not sure this will fix your problem though, as it seems unlikely that the spring:url tag is adding the contextPath twice. That seems more likely to be happening somewhere else in your code. Either way this should help you find out.

Related

Issue on Camel route - parsing XML tags

I have a complex camel route, which starts with an initialization route, which tries to set the headers with the info from the XML used as input.
I wonder how the route is not being able to parse the XML content, using XPath.
Before calling the route, I print the xml information in my java JUNIT, and it prints correctly, with all xml tags.
So I know the information is being sent as I am expecting.
But that route, which should set the headers using XPath, returns empty to any expression I try to use! I even used a XPath tool to assist me (https://codebeautify.org/Xpath-Tester), to check if was some xpath coding mistake, but I get the results I want from there.
So, let's suppose, I have an XML as:
<bic:Test>
<bic:context>
<bic:memberCode>GOOGLE</bic:memberCode>
</bic:context>
</bic:Test>
So, with the line below:
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag>/text()</xpath>
</setHeader>
or
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag></xpath>
</setHeader>
I will see the header with empty content.
I tried so many different things, that finally I decided to print the all the content, using an XPath expression as /.
It will print only the content ("GOOGLE"), not the tags.
Could you please assist me?
Thank you in advance!
This is probably a namespace related issue.
You have to define the bic namespace in the camel context and then use it in the xpath expression.
Have a look at the documentation in https://github.com/apache/camel/blob/master/camel-core/src/main/docs/xpath-language.adoc and particularly in the example of "Using XML configuration"
Also look at "Namespace auditing to aid debugging" for further information about debugging namespace related issues in camel.

Use of spring security jstl tag <sec:authority access="hasPermission(#domain, 'permission')> inside a jstl core loop

I'm using facelets (JSF2.1) and I'm trying to do something like:
<c:forEach var="domainObject" items="#{MB.listOfDomainObjects}">
<sec:authorize access="hasPermission(#domainObject,'PERMISSION_X')">
hello world
</sec:authorize>
</c:forEach>
I've tried changing #domainObject by #domainObject, #{domainObject}, $domainObject and a lot of other combinations with the same result: domainObject is not processed correctly. Sometimes I get an error saying the page cannot be constructed, others domainObject is null.
It's like the scope where the tag c:forEach puts the variable domainObject is not scanned by sec:authorize to find it.
I've tried also to force the use of a scope using the tag <c:set ... scope="view"/>. I've also tried to use <ui:repeat> with the same results, but considering the tag sec:authorize is a jstl one, I suppose is executed in build time (like c:forEach) and not in render time (like ui:repeat), so I think I must use c:foreach and not ui:repeat.
Any chance to solve my problem?

Multiple c:imports with Stripes is causing problems

I'm having a problem when I call the same Stripes action with multiple c:import tags in the same jsp. When I use the first c:import, I use a few c:params with it. These get bound to the corresponding fields in the action. But then when I use the next c:import, the fields are already set from the first c:import, which is not what I want. I want to be able to import an action several times, and each time it should only use the values I pass in with the c:param tags.
The only solution I could think of is to call a method before binding and validation takes place, that sets all the fields to null. Is that a bad idea? What's the best way to handle this?
<c:import url="/widget/House.action">
<c:param name="dogNam" value="Muffin" />
<c:param name="catName" value="Junior" />
</c:import>
<c:import url="/widget/House.action">
<c:param name="dogNam" value="Rocky" />
</c:import>
In this example catName is getting set to "Junior" both the first and second time I use the c:import.
The c:import fires an http request to your Java application server and from what you describe the second c:import still seems to add the catName parameters to the request, you might want to try this:
<c:import url="/widget/House.action">
<c:param name="dogNam" value="Rocky" />
<c:param name="catNam" value="" />
</c:import>

What might be causing this IllegalStateException discrepancy in JSP/Spring?

I had a JSP file with a c:redirect tag that would forward along a user to another page.
<!-- Yes, I know this loop is probably unnecessary, but I'm not fluent in jsp and have determined it is not the problem. :) -->
<c:if test="${cmd.numberOfResults == 1}">
<c:forEach items="${cmd.matches}" var="someVar">
<c:redirect url="/loadThatResultInfo.html"/>
</c:forEach>
</c:if>
The old implementation of the command object is needs updating (where I come in). The way I'm doing so is by creating a generic "search result" object which contains an instance of that old object (for now). I get that instance through a property in that generic class, so my code is now this:
<c:if test="${cmd.genericSearchObject.numberOfResults == 1}">
<c:forEach items="${cmd.genericSearchObject.matches}" var="acct">
<jsp:forward page="/loadThatResultInfo.html"/> <!-- new try! -->
<c:redirect url="/loadThatResultInfo.html"/> <!-- old try... -->
<% response.sendRedirect("/loadThatResultInfo.html"); %> <! new try! -->
</c:forEach>
</c:if>
Each of those three tries all result in IllegalStateExceptions of some sort. Why does this change cause the exception, especially considering that the lines involved -- the redirect, not the changed/bound class instances -- are causing the problem?
Back-end changes were made accordingly, referencing the property within my new encompassing "generic" class to satisfy the old functionality. I know this works because all related functionality, beside what I'm writing about, works.
Research online indicates:
- I can't redirect/forward after a submission has already been submitted. Then how was I able to do it before?
- Attempt to flush an already-cleared buffer causes this. What changed that makes it cleared now as opposed to the older (first) implementation?
- The size of the page's buffer needs to be bigger. THIS is one I don't understand and would really love for the stackoverflow community to address; I can see my new class causing size changes that would need changes to be dealt with.
------- ANOTHER ANSWER! -------
First and foremost, ALWAYS SET UP THE SITUATION IN THE CODE as described by the marked answer. However... if you're stuck and don't want to do that, here's a quick fix: javascript!
<script type="text/javascript">
location='./yourPageToGoTo.html'
</script>
JSP is part of the response. You're attempting to change the response destination in a JSP instead of in a controller. If you do this halfway in a JSP, then it's too late, because the HTTP response headers may already have been sent (the response is then in committed state). This is a point of no return and an illegal state for changing the response. It's too late then. Any attempt will result in the servletcontainer to throw IllegalStateException: response already committed.
To fix this, you need to put this piece code in the very top of JSP file and pray that the response hasn't already been committed at that point (which will usually happen after writing about 2KB of data to the response, depending on the servletcontainer config). However, JSP is still the wrong place for the job, you should rather do this in the controller, before forwarding the response to the JSP (or to instruct from within the model the controller somehow to do the job, when you're using a MVC framework).

how spring mvc tag works?

I am trying to write some kind of raw html to mimic what spring mvc tag produces after page rendering(and I do make them look exactly the same if you open them with a html element inspector). as I want to create dynamic input form using javascript. but it didn't work. it seems I had to use only what it offers: e.g. <form:input path="firstName" />.
to get the data binding working.
I thought the tag lib only help you to produce a html block that spring knows how to handle them in backend (action). from a web http perspective. what else it can send beyond a bunch of form data, and they should send the same thing. so I am really curious to learn what magic thing the tag lib dose beyond producing a html block.
the other thing I would like to know is where the model object is being hold when the form is being submit to a matched action. you know, you can get the model attribute by using #modelAttribute as the input parameter. is it in the original request object? or in the ActionRequest to which the dispatcherServlet build and put it. or even somewhere else?
thanks in advance.
I got it figured out. the raw html just works as spring tag does. as long as you are in the form tag block. you are okay to use raw html such as
<input type="text" id="abc" name="abc"/> just make sure name reflect your bean attribute path.
id is not mandatory and is just helping you to identify the very element. I guess I missed something when I work with the raw html by the time I ask the question. hope this helps for guys working with raw html approach, especially in case of dynamic input creation.

Resources