Add html content to faces message from Backing Bean [duplicate] - jsf-2

This question already has answers here:
How do I add HTML code to JSF FacesMessage
(4 answers)
Closed 7 years ago.
I need to add a Html content when some method returns the wanted result (any method, any result) to the JSF page exactly like primefaces do with .
Html to add is simple :
<div class="alert alert-success" role="alert">
<strong>Well done!</strong> You successfully read this important alert message.
</div>
But can't find a way to do this cleanly, I've read many posts like How do I return HTML from managed bean in JSF? but my bean need to insert that html when user is doing something e.g. submitting a form result.
Also : is there a way to save it and show it in case of a redirection like it is with primefaces context.getExternalContext().getFlash().setKeepMessages(true);

Why not simply using h:messages tag ? It is fully customizable in terms of design and you can simply affect your styles and class without all the turn-around
Source : http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_messages.html

Related

How do I add session variable to view markup? [duplicate]

This question already has answers here:
Accessing a Session object from Razor _Layout.cshml
(2 answers)
Closed 7 years ago.
I am trying to add a session variable to html markup using MVC Razor, I tried this:
<div class="panel-body">
<p>It might suprise you to know that you can upgrade from #Session("CurrentProvider") to blah blah....</p>
</div>
I tried wrapping it in code tags and all sorts. How can I fix this?
Seems the answer is to append with ToString
#Session("CurrentProvider").ToString

MVC - How to render a 'a href' link in a View that's been stored in a database?

Question background:
I have an MVC4 app whos homepage data is supplied from a View Model.
The Issue:
Within the View Model I have a property called AboutUsDescOne This is set from the Controller and Action Method from data that has been retrieved from a database. Within the data set on the property is a a href link, The following shows the text stored in the database:
This is the website Google Google click the link.
The issue I'm having is when I render the data in the View the a href is being seen as text in the View and is not showing as a link, as shown:
The Code:
The propety is simply set as a propety on a standard ViewModel:
public string AboutUsDescOne {set; get;}
This is how it is then rendered in the View:
<div class="col-md-8 form-group">
<p>
<h4>#Model.HomePageVM.AboutUsDescOne</h4>
</p>
</div>
Can anyone tell me how to get the link to render in the View?
Try this:
<h4>#Html.Raw(Model.HomePageVM.AboutUsDescOne)</h4>
Use this helper with caution though. If a user can edit this block of HTML then you might be making yourself vulnerable to an XSS attack:
Text output will generally be HTML encoded. Using Html.Raw allows you to output text containing html elements to the client, and have them still be rendered as such. Should be used with caution, as it exposes you to cross site scripting vulnerabilities.

<ui:include> with dynamic src ... complete madness [duplicate]

This question already has answers here:
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)
(3 answers)
Closed 5 years ago.
I'm having a massive problem getting an extremely simple task done in JSF. The problem: I have objects, who have aggregated properties that can vary in type from object to object. Depending on the type of the property, I want to use a different set of input fields.
The subtype components reside in frameworks and get loaded on demand. To this end, I use the following code:
<h:panelGroup id="zusatzdaten">
<fieldset class="clear">
<legend>#{tickerUI.ticker.tickerDescription.label}
(#{tickerUI.ticker.tickerDescId})
</legend>
<h:panelGroup rendered="#{tickerUI.editComponentName != null}">
<ui:include src="#{tickerUI.editComponentName}"/>
</h:panelGroup>
</fieldset>
</h:panelGroup>
The name of the component comes out of TickerUI, which is of #SessionScope. Now the dazzling bit: when it first loads, the correct subcomponent is displayed. However, when using a link in the navigation, which should lead to including a different component, the content is NOT updated! This results in an error, because the data is now a different subtype, but the form components are still from the previous one.
When going back from the error and clicking the link again, the correct component is displayed. I logged the value of editComponentName and the correct values are returned. This is very confusing. Why is including the wrong content when the getter returns the correct component name to the 's src attribute?
Help greatly appreciated.
Actually your problem is a classic view build vs view render time problem/misunderstanding. More specifically, the view is built on every new request and reconstructed from a previously saved state on postbacks. Later the view is rendered to produce HTML content.
Now, as <ui:include> is a tag handler, or in official terms a view build time tag, when you first request the page, its value attribute is evaluated, and the included page makes its way into the view. Upon postback, contrary to what you might expect, the incuded contents are already there, in the reconstructed view. So, it is an expected behaviour that you'll have the exact part of view rendered.
As to the solution, you could just simply include an exhaustive list of static includes that are wrapped in conditionally rendered <ui:fragment> JSF components. For simplicity, all content could be places within a container like <h:panelGroup> that's always rendered for ease of AJAX updates. The code could be assembled as follows:
<h:panelGroup id="ui">
<ui:fragment rendered="#{bean.condition1}">
<ui:include src="/path/to/file1.xhtml"/>
</ui:fragment>
...
<ui:fragment rendered="#{bean.condition_n}">
<ui:include src="/path/to/file_n.xhtml"/>
</ui:fragment>
</h:panelGroup>
A classical dilemma, it seems. BalusC's blog gave the solution for my case in the form of a configuration parameter in web.xml:
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>

Primefaces, captcha and FacesMessage

I have integrated reCaptcha component in my JSF view (Primefaces) and I want to gather reCaptcha fails errors (blank or error values).
I have to use <p:messages/> component with for attribute because I have many <p:messages/> components (1 for tab in <p:tabView/> component) but when I define id attribute in my <p:captcha/> component and use that id in the for attribute of the <p:messages/>, it doesn't work. When I don't use for attribute to test, I see FacesMessage like :
j_idt16:j_idt69 : bla bla bla...
but j_idt16:j_idt69 id doesn't seem to exist in my generated HTML code...
If I use for="j_idt16:j_idt69" it doesn't work too...
So how can I dispatch captcha messages into proper messages component please ?
The Captcha provide by Primefaces didnt work for me. Please follow the following tutorial which solved my captcha problem using JSF 2 /Primefaces.
http://techzone4all.wordpress.com/2012/02/20/integrate-simple-captcha-to-web-project/
To address your specific need, you should define values for the validatorMessage,converterMessage and requiredMessage attributes on the <p:captcha/>, to be able to display user-friendly messages for their respective situations. FYI the <p:captcha/> component is processed during the submit of it's enclosing form, i.e it cannot be attached to a specific component or anything like that.
For clean message display, you should only place the captcha component in a <h:form/> along with other components relevant to the reCaptcha operation.

Rendering html using Struts2

Using Struts2, my goal is to present a simple blog to a user using Struts2 iterators, such as:
Most Recent Topic
response 1
response 2
...
Previous Topic
response 1
response 2
...
Users generate and submit each Topic/Response using a separate form, but, once submitted, I don't want them to edit the blog.
To generate either a Topic or a Response, I provide an editor (like the stackoverflow editor I'm using now) that produces html-formatted text, including whatever styling (bold, underlines, lists, etc.) that the user chooses. The text of the Topic/Response created by the user, including the html tags, is stored in a database.
However, I cannot find a way to render the Topic/Response as html in the blog. For example, text bolded in the editor shows up as <strong>text</strong> in a struts2 s:textarea tag.
I know that the s:property tag has an 'escapeHtml' attribute that will prevent this, but the s:property tag can't layout the text properly, and it seems that only the s:property tag has this attribute.
I've tried using <input value="%{#topic.content}" /> within the iterator instead of s:textarea, but it doesn't seem to recognize the #topic iteration reference.
Is there a way to do this?
use text instated of tax area .Let me know if you still facing this issue.
Use escapeHtml="false". I just tried it myself and it works as intended.
For example, with:
<s:set var="var1"><p>some stuff</p><p>other stuff</p></s:set>
<s:property value="var1" escapeHtml="false" />
renders the paragraph tags as you would expect.
How about using <pre> with <s:property>.
About html <pre> tag:
http://www.w3schools.com/tags/tag_pre.asp

Resources