getter doesn't work when converted from textfield to property in Struts 2 - properties

I had a textfield in Struts 2 defined as
<s:textfield key="fName" label="First Name" readonly="true" />
and the getters were working fine, but since it was supposed to be only a display only field, I changed it to
<div> <br /> First Name: <s:property value="fName" /></div>
and now the getter gets a NullPointerException.
Is there a way to circumvent this error? thanks
getter in Person.java:
public String getFName() {
return fName;
}
stacktrace:
test.model.Person.getFName(Person.java:43)
test.service.PersonDAO.update(PersonDAO.java:182)
test.action.UpdatePersonInfo.execute(UpdatePersonInfo.java:46)

You have to be careful choosing class variable name, in you case fName. With new version of struts 2 which uses newer version of OGNL, some of the getter/setter are not working properly. This is because newer OGNL follows Java bean specification. So your getter would be
public String getfName() {
return fName;
}

Related

Use a #typeparam as component in Blazor

Is it possible to use a #typeparam as Component?
More explicitly, do something like the following MyComponent.razor:
#typeparam TComponent
<TComponent />
Of course, there would also be a MyComponent.razor.cs file whose content would be:
public partial MyComponent<TComponent> : ComponentBase where TComponent : ComponentBase
so that the compiler would know that <TComponent /> is meaningful.
I cannot find any documentation about this in Microsoft docs.
When I try it seems to compile, but display the following warning:
warning RZ10012: Found markup element with unexpected name 'TComponent'. If this is intended to be a component, add a #using directive for its namespace
However it is only a warning and not an error. It does't show anything in the browser though.
I am using ASP.NET 5.
Thanks
Not quite sure what you are trying to do but you can write what I believe you are trying to achieve as a component class like this:
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
namespace BlazorTest.Pages
{
public class ComponentRenderer<TComponent> : ComponentBase where TComponent : IComponent
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenComponent<TComponent>(1);
builder.CloseComponent();
}
}
}
So in Index.razor you can do something like the following to render a component of type FetchData:
....
<ComponentRenderer TComponent="FetchData"></ComponentRenderer>
....
If I'm wide of the mark post a little more information.

ASP.NET Core MVC : model binding and complex types

I have a model that has a property which is a class (complex type) which, in turn, has properties that are themselves complex types; turtles all the way down.
I feel that I ought to be able to do
<input type="hidden" asp-for="#Model.MyComplexType" />
But apparently not so out-of-the-box.
Is there a simple solution? I really don't want to have to (can't be arsed) to write out an <input type="hidden"> for every property.
You can mark your complex type's properties with the HiddenInput attribute:
public class ComplexType
{
[HiddenInput]
public string Property {get;set;}
}
Then in your view call
#Html.EditorForModel(x => x.MyComplexType)
This will add all your properties as hidden.

JSF action method with variable parameter [duplicate]

This question already has answers here:
Invoke direct methods or methods with arguments / variables / parameters in EL
(2 answers)
Closed 4 years ago.
How do I call method with variable parameters in JSF?
I tried something like this:
<h:commandButton value="Send" action="#{myBean.checkPIN(someOtherBean.PIN)}" />
However, this doesn't work.
If you are using EL 2.2+, it's possible.
If you are using older version ot EL, you can use do the following:
<h:commandButton value="Send" action="#{myBean.checkPIN}" />
<f:param name="parameter" value="123" />
</h:commandButton>
In the managed bean you can retrieve it like:
public void checkPIN() {
...
Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
String param = parameterMap.get("parameter");
...
}
Yes it is possible if you are using > EL 2.2 which is part of Servlet 3.0.
See #BalusC's suggetions here Invoke direct methods or methods with arguments / variables / parameters in EL
It does work with EL 2.2. Which is probably the version you're using, since you're using JSF 2 (Even though it might not be the case).
You can do a very simple test. You can have an OtherMB such as this:
#ManagedBean(name = "otherMB")
public class OtherMB{
public String getValue(){
return "Other Managed Bean Value";
}
}
And a method in your MainMB like this:
#ManagedBean(name = "mainMB")
public class MainMB{
public void method(String str){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(str));
}
}
And in your xhtml you can just invoke the function using a button:
<h:commandButton action="#{mainMB.method(otherMB.value)}" value="Click Me!" />
Just remember that the h:commandButton needs to be inside an h:form, and that you need a component to show the message. Or you can just change the implementation to print the message in the console

MVC-3 and Dynamic - #Html.Label(View.X) not Rendering

Using MVC-3, Razor:
-- MyController --
public ActionResult Index(String message) // where message = "hello"
{
ViewModel.Test1 = "This is a test";
ViewModel.Test2 = "This is another test. " + message;
}
-- Index.cshtml --
#Html.Label((string)View.Test1)
<br />
#Html.Label((string)View.Test2)
Why will it only render out the following?
<label for="This is a test">This is a test</label>
<br />
It's been driving me absolutely crazy over the past few days and seems to make no sense. There has to be a reason for it.
I can debug this and step through thew view. In the view, I watch as this line is processed and the value of View.Test2 is "This is another test. hello".
I have cases where I am doing the following and it works fine.
(ex)
ViewModel.Something = this.readDataService.GetSomething();
What's the difference?
Thanks,
Rob
Looks like you are using a pre-RC2 version of ASP.NET MVC 3. ViewModel was changed to ViewBag in RC 2 (see the this post by Scott Guthrie).
With earlier previews of ASP.NET MVC 3 we exposed this API using a dynamic property called “ViewModel” on the Controller base class, and with a dynamic property called “View” within view templates. A lot of people found the fact that there were two different names confusing, and several also said that using the name ViewModel was confusing in this context – since often you create strongly-typed ViewModel classes in ASP.NET MVC, and they do not use this API.
With RC2 we are exposing a dynamic property that has the same name – ViewBag – within both Controllers and Views.
And it does look like you are trying to use ViewModel as the strongly typed model for your view. Instead, create a class to use as your model and then use #Html.LabelFor:
public class PersonModel
{
public string Name { get; set; }
}
in the controller:
PersonModel model = new PersonModel { Name = "John" };
return View(model);
in the view:
#Html.LabelFor(model => model.Name): #Html.TextBoxFor(model => model.Name)
which renders:
<label for="Name">Name</label>: <input id="Name" name="Name" type="text" value="John" />
HTH

Dynamic property defaults in CF9 ORM

How do you set up dynamic property defaults on CF9 ORM objects?
For instance, I know I can set a property default like this:
property name="isActive" default="1";
But what if you want to have a dynamically generated default, such as a date or a UUID?
property name="uuid" default="#createUUID()#";
...throws an error - so what's the workaround for this?
When an Entity object is created the objects constructor is called. This is a great place for running "setup" code.
User.cfc
component persistent="true"
{
property name="id" fieldtype="id" generator="native";
property name="secretKey";
public User function init() {
if (isNull(variables.secretKey))
setSecretKey(createdUUID());
return this;
}
}
Have you tried overloading the getter?
public string function getUUID() {if(variables.UUID EQ ""){ return createUUID(); } else { return variables.firstName; }; }
I can't test that from where I'm at, but I would try.