How to set struts ActionForm attributes using <html:link>? - attributes

I need to do the thing like this (setting ActionForm attribute):
<html:form action="Link.do?method=editNews">
<html:hidden property="idNews" value="${news.newsMessage.idNews}" />
<html:submit value="EDIT"/>
</html:form>
But in <html:link> or regular a-href tag. So I don't want this parameter to apear in my link as a request parameter. Is it possible?
P.S. idNews is the parameter in my ActionForm class and it has setter and getter.

No, it's not possible. A link performs a GET request, and the only way for a GET request to send information to the server is to use request parameters, which appear in the URL.
The only thing you can do is to have your link invoke a JavaSCript function which submits a hidden form using POST. But it's ugly.
Why do you fear by making the parameter visible in the URL? Are you aware that anybody can view the source of your HTML page and see the hidden field here?

Related

How to open JSF page and pass parameter by href link?

Now I have one website. I want to provide user one link with one parameter. Then he can access the website and query something according the parameter. I want to the link is "http://10.182.20.86:8080/webproject/main.xhtml/scenarioid=1234" etc.
Now I use rest to pass the parameter to Java. But I can't get instance from FacesContext. It always returns null, when I use FacesContext.getCurrentInstance() in Java. Do you know how to implement the function by link with parameter in JSF? Thanks.
I have solved it by create one class and it extends HttpServlet. I get the parameter by request and get bean attribute by HttpServletRequest getSession. Now it works. Thanks, Balus.

Init method is not filled in even with parameters

I have lost a few hours trying to figure out why I can not retrieve the parameters of my link. Following this question, I did my link with passing parameters. My problem is relatively easy, if I can get the parameters in the popup that opens when the user clicks the link. Except that I can not get them.
My class has a method annotated with #Create that every time the object is initialized it executes this method.
The only way I can get the idsess_ parameter is in the URL, but I can not pass any parameters I want the URL.
In short, by clicking on my <a:commandLink> wish the <f:param>'s were passed to the bean in a matter and the same to process according to these parameters.
Edit: changed my code and created my own action. In this case I was reusing an action of another view. I recreated my action and used the method described in the answer that was accepted
Not sure what your <a:commandLink/> component is? (Im assumed you re-mapped a4j:commandLink somehow?)
I think the problem you are having is that the #RequestParameter variables are only populated when the page first renders, when you click the commandLink you are performing a postback so the values are lost.
You should try doing a redirect instead (ie. using <s:link> with <f:param> rather than commandLink) or you can pass the parameters using pages.xml to bind the request params, using this markup:
<page view-id="/your-page.xhtml">
<param name="pId" value="#{bean.variable}" required="false" />
<action execute="#{bean.action}" />
</page>

SharePoint SaveButton RedirectURL attribute doesn't do anything. Why?

I have a SharePoint SaveButton in my EditFormTemplate and I don't want the browser to redirect to the default location which is the List View (i.e. AllItems.aspx). I instead want the user to be redirected to another page.
Many other people seem to have the same issue. I do not wish to replace the SharePoint SaveButton with a standard aspx button control and use JavaScript as this presents it's own set of problems and frankly I don't think that this sort of functionality deserves a JavaScript hack.
If an attribute called "RedirectURL" does in fact exist for the SharePoint SaveButton (which it does) then why on Earth would it not simply redirect the browser to the specified URL?
<SharePoint:SaveButton ID="SaveButton1" runat="server" RedirectUrl="[My Custom URL]" />
Why doesn't the code above do what I want it to do?
You need to use a JavaScript call ddwrt:GenFireServerEvent('_redirect={URL}'.
Take a look at the example below:
<input type="button" class="contact-button" value="Save" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={example.aspx}')}" />
Hope this helps.
OK. I figured it out.
I created a SPListItemEventReceiver and set the ItemUpdated Synchronization element in the Elements.xml file to "Synchronous".
<Synchronization>Synchronous</Synchronization>
I then followed the instructions outlined here.
Since I needed to perform the redirect after an Update I didn't have to do all of the hacky stuff to try and perform an Updated async operation in the Updating async event as described in the link above for ItemAdded and ItemAdding.
I simply added a private HttpContext property called currentContext, added a default constructor where I set the currentContext property to HttpContext.Current and then did the SPUtility.Redirect in my ItemUpdated event using the value stored in the currentContext property that was set in the constructor.
I've had problems with the SaveButton if the URL of the form it is used on includes a Source parameter. I don't recall details but my fix was just not to include that parameter when opening the form.
Sounds like a different problem for you, but that may help someone.
Dave

JSF: Bookmarkability with ViewScoped

I am trying to make my app "Bookmarkable", and i am using view parameters to achieve it.
And i think i still do not get the right way to do it right in JSF, even after reading this, and many others.
My problem is that the get parameters get lost after any non-ajax postback, i mean, the parameter value is still set in the bean and the app works correctly, but it gets removed from the URL making the URL invalid.
For instance, having an URL like http://company.com/users?id=4, as soon as that page executes a non-ajax postback (for uploading data, for instance) the URL becomes just http://company.com/users. The app continues to work correctly, but the link is not any more "Bookmarkable".
Is there any way to prevent the non-ajax postbacks removing the viewParams from the URL?
My use case is to be able to bookmark a page to EDIT an object, and there i need to be able to upload data (if not i would not use non-ajax postbacks). I know i would not need any postback if i would want to bookmark the page to only VIEW the data of the object, but that is not my case.
I could also do a redirect to the same page with the same params, and let the app to recreate the view scoped bean, but then i really do not see any benefit over request scoped beans...
Any suggestion is very appreciated.
This behaviour is "by design". The <h:form> generates a HTML <form> element with an action URL without any view parameters. The synchronous POST request just submits to exactly that URL which thus get reflected as-is in browser's address bar. If you intend to keep the view parameters in the URL, while using ajax is not an option, then you basically need to create a custom ViewHandler which has the getActionURL() overridden to include the view parameters. This method is used by <h:form> to generate the action URL.
public String getActionURL(FacesContext context, String viewId) {
String originalActionURL = super.getActionURL(context, viewId);
String newActionURL = includeViewParamsIfNecessary(context, originalActionURL);
return newActionURL;
}
Or, as you're based on the comments already using OmniFaces, you could also use its <o:form> component which basically extends the <h:form> with the includeViewParams attribute which works much like the same as in <h:link> and <h:button>.
<o:form includeViewParams="true">
...
</o:form>
This way all <f:viewParam> values will end up in the form action URL.
See also:
Handling view parameters in JSF after post

JSF-Calling BackingBean method twice maintaining value of inputFileUpload

I am very new to JSF. I have the following requirement:
On click of a commandButton, call a backing bean method to check if there is some data present satisfying the condition.
If yes, confirm from user for overwrite.
If user says OK, call the same method of backing bean with some parameters set to tell the program to overwrite the data.
What I am doing is:
having action of the commandButton as the method name.
in the backing bean method, check if we have come with certain condition, check if the data is already present.
If yes, go back to page and ask for confirmation.
If confirmed, call the click method of the button.
The problem is, when I come back to the page, the inputFileUpload component on the page loses its value.
What can I do to achieve this? Please help.
This is fully by HTML specification and completely outside control of JSF. It's by HTML specification for security reasons not possible to (re)display the value of a HTML input file field with a value coming from the server side. Otherwise a hazard scenario as shown in this answer would be possible.
You need to redesign the form in such way that the input file field is not been updated during confirmation. You can use among others JavaScript/ajax for this: just submit the form by ajax and make sure that the input file field is not been updated on ajax response.

Resources