jsp content:
<s:checkboxlist list="list" name="values"/>
action content:
public List<Foo> getList() {
return list;
}
public String[] getValues() {
return values;
}
public void setValues(String[] values) {
this.values = values;
}
class Foo:
private String code;
public String getCode() {
return code;
}
public String toString() {
return code;
}
When I put breakpoint at getValues() method, I clearly see it's being called with some values there. But that values don't appear to be selected on a page.
What am I missing here?
I found a solution. I've added
<s:checkboxlist list="list" name="values" listKey="code" listValue="code />
to jsp and it all started working after that. It generates the same html, but it seems that despite rendering correctly, that properties are required by struts to check what values should be set. And there is no emphasis on that in struts docs.
EDIT:
It seems that only this is actually requited for this thing to work:
<s:checkboxlist list="list" name="values" listKey="code"/>
I wonder if i can replace "success" with a Constants value from a JSF library.
Backing Bean Method:
#Override
public String save() {
LOG.info("Called");
return "success";
}
For your issue, you'll find Omnifaces' <o:importConstants /> so useful (that's what I use in my own projects). That way you can import your constants file in your JSF page (I use my master page template for that).
<o:importConstants
type="com.mycompany.NavigationResults" />
This way you can access your NavigationResults values both from Java code and JSF tags (EL scope).
public abstract class NavigationResults {
public static final String SUCCESS = "success";
public static final String HOME = "home";
}
Use it in your managed beans:
public String save() {
LOG.info("Called");
return NavigationResults.SUCCESS;
}
In your buttons or links:
<h:button value="Go home" outcome="#{NavigationResults.HOME}" />
<p:editor value="#{editorBean.value}" widgetVar="editor" width="686"
height="390" language="en" align="center">
</p:editor>
Following is my rich-text editor bean picked up from primefaces
#ManagedBean(name = "editorBean")
#SessionScoped
public class EditorBean {
private static final String MANAGED_BEAN_NAME = "editorBean";
private String value;
public static EditorBean getCurrentInstance() {
return (EditorBean) FacesContext.getCurrentInstance()
.getExternalContext().getRequestMap().get(MANAGED_BEAN_NAME);
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Apart from this I have another bean say A. I have a method inside A that populates a HTML table. What I want is when the user opens the editor, it should be pre-populated with that HTML table data and of course the changes should get reflected into (String: value). Therefore, you can say that I am trying to tie up both the values together. I think it needs to be done with DI but somehow its not working. If someone can guide or quote an example, it would be really helpful.
One way to do it is rewrite your getValue() method to pick up the value from bean A.
And yes, the reference to your A bean should come from DI:
//injecting a reference to A
#ManagedPropery(value="#{A}") //or whatever is the name of your bean
private A beanA;
public void setBeanA(A beanA) {
this.beanA = beanA;
}
Or, with CDI, just:
#Inject private A beanA
Finally, your getValue method
public String getValue() {
return beanA.getValue()
}
I am using struts2 and i have property and want to make it a
mailto link.
Can anybody help me how to get it.
thanks
Assuming an action such as:
class SomeAction {
private String email;
// ...
public String getEmail() {
return email;
}
}
You can access it in the JSP using the following:
Struts 2 Tag: <s:property value="email"/>
JSP EL: ${action.email}
I'm trying to create an ActionFilter to replace some text in my HTML. Basically when the server is using SSL I want to replace references to my CDN (http://cdn.example.com) with references directly to my server (https://www.example.com). So the structure is something like this (I assume OnResultExecuted is where I should start):
public class CdnSslAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if(filterContext.HttpContext.Request.IsSecureConnection)
{
// when the connection is secure,
// somehow replace all instances of http://cdn.example.com
// with https://www.example.com
}
}
}
This would be used in my secure controllers:
[CdnSsl]
public class SecureController : Controller
{
}
The reason I want to do this is my CDN doesn't support SSL. And there are references in the Master pages to CDN resources. Example:
<link href="http://cdn.example.com/Content/base.css" rel="stylesheet" type="text/css" />
I ended up using a variation on this blog post:
http://arranmaclean.wordpress.com/2010/08/10/minify-html-with-net-mvc-actionfilter/
with my own filter:
public class CdnSslAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.HttpContext.Request.IsSecureConnection)
{
var response = filterContext.HttpContext.Response;
response.Filter = new CdnSslFilter(response.Filter);
}
}
}
Then the filter looks like this (some code omitted for brevity):
public class CdnSslFilter : Stream
{
private Stream _shrink;
private Func<string, string> _filter;
public CdnSslFilter(Stream shrink)
{
_shrink = shrink;
_filter = s => Regex.Replace(s,#"http://cdn\.","https://www.", RegexOptions.IgnoreCase);
}
//overridden functions omitted for clarity. See above blog post.
public override void Write(byte[] buffer, int offset, int count)
{
// capture the data and convert to string
byte[] data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string s = Encoding.Default.GetString(buffer);
// filter the string
s = _filter(s);
// write the data to stream
byte[] outdata = Encoding.Default.GetBytes(s);
_shrink.Write(outdata, 0, outdata.GetLength(0));
}
}
I don't know but the answer from #Haacked at this question could help.
Performing replacements on the generated output inside of an action filter would be a bit complicated.
An easier approach (if you can edit your master pages) would be to write a new Html helper method (similar to the Html.Content() helper) that would conditionally emit the correct url. If you wanted that replacement to happen only for certain controllers then you could still have an action filter, but all it would do would be to set some flag in Request.Items and your helper could check that flag.
My suggestion is to follow #marcind's approach, one possibility is to use a custom extension method to generate the correct url depending on the current url scheme.
public static MvcHtmlString CdnActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName)
{
if(helper.ViewContext.HttpContext.Request.IsSecureConnection)
{
return helper.ActionLink(linkText, actionName, controllerName, "https", "www.yourhost.com"...);
}
return helper.ActionLink(linkText, actionName, controllerName);
}
One drawback of this approach is that you'll need to replace all your current ActionLink invocations in your views (or at least the ones you need) with an invocation to this extension method.