Calling Methods and Functions - jsf-2

in jsf2 if we have some function like
public String greeting(String gtr) {
return "Hello " + gtr;
}
then from jsf page we can call this function as
<h:outputLabel value="#{greetingBean.greeting['some-name']}"
now my question is i want to pass the dynamic parameter from the same bean class instead of 'some-name' something like value="#{greetingBean.greeting[greetingBean.name]} is it possible ?
i required this because i have value in Map<String, ArrayList> and want to edit particular ArrayList value ?
any suggestions ?

Brackets are used to replace the dot notation. That is,
#{greetingBean.greeting['some-name']}
is the same as
#{greetingBean.greeting.some-name}
So, the greeting method will not be called. Instead, JSF will try to access a greeting property, and then try to access a some-name property of the object returned by greeting property. That is, your code above is already generating an error.
If you are using EL 2.2 and want to call an action, simply put:
#{greetingBean.greeting('some-name')}
or
#{greetingBean.greetingThatReceivesAMap(greetingBean.name)}

Related

How to get the list in jsp present in reques using s:select tag struts2 [duplicate]

I am developing a simple struts application. In my JSP I have a dropdown list box (using s:select tag). I need to fill the values with a arraylist values in the action class. How can I do that? what changes needed in the structs.xml file for complete this?
JSP:
<s:select name="department" label="" list="departmentlist" headerKey="-1" headerValue="Select Department">
Action class:
private List<String> departmentlist = new ArrayList<String>();
public String xyz()
{
departmentlist.add("aaa");
departmentlist.add("bbb");
departmentlist.add("ccc");
departmentlist.add("ddd");
return "success";
}
The error
"The requested list key 'departmentlist' could not be resolved as a
collection/array/map/enumeration/iterator type. Example: people or
people.{name} - [unknown location] "
means that the select tag is not able to resolve departmentlist as a collection. It is an OGNL expression which is trying to find the departmentlist in the value stack and if it not found or contains a null reference the select tag will complain. When you render the select tag make sure the list is in the value stack and is initialized. See the example here.
Try to add get and set method for our field departmentlist,in your class
Exple :
in your class controller put this method for your field departmentlist :
public List<String> getDepartmentlist(){
return this.departmentlist();
}
public void setDepartmentlist(List<String> departmentlist){
return this.departmentlist = departmentlist;
}

How to Dispatch Action types in Struts 2

I am trying to find the replacement for org.apache.struts.actions.DispatchAction.types in Struts2.
Below code snippet:
if(types != null)
{
if(types.length == forward.length)
{
select.add(type[0]);
}
}
The forward is a string type object and select is a list type array.
The documentation for DispatchAction.types:
types
protected java.lang.Class[] types
The set of argument type classes for the reflected method call. These are the same for all calls, so calculate them only once.
There's not the same thing in Struts2. You should refresh your mind to understand that struts2 works differently than struts-1.
For example you can map your actions directly to the method. You can use SMI for method call like in this answer
You should place the annotation directly on the method. Because if you put it on the class the default method execute() is used for the mapping.
#ParentPackage("basePackage")
#Namespace("/")
#AllowedMethods("test")
public class UserAction {
#Action(value = "userAction")
public String test() {
// your code
rerurn Action.SUCCESS;
}
}
or use wildcard mapping like this answer.
The action name is overridden if used in the same package. The action name maps to a specific method or execute.
You can use wildcard mapping to put a method name in the action.
<action name="*product" class="com.DispatchAction" method="{1}">

java struts2 very strange behaviour while using iterator

public class UserAction{
private UserData user;
//getter, setter
public String Load() {
user = UserDao.getInstance().getItem(getUserContext().getId());
request.getSession().setAttribute("item", user);
return super.Load();
}
}
public class PropertyAction {
private List <PropertyData> propertyList;
//getter, setter
#Override
public String execute() throws Exception {
propertyList=PropertyDao.getInstance().getItems();
return "list";
}
}
jsp:
<s:iterator value="propertyList" var="item">
${item.name}
${item.thema}
${item.desc}
</s:iterator>
I want to show very strange behaviour of Struts2.
I click property link -> then run PropertyAction.execute() and it display above jsp.
I click user link -> then run UserAction.Load()
I click property link -> then run PropertyAction.execute() and error has been shown "UserData has no property thema".
I spy what happened and I notice that I set setAttribute with name "item". So if I use var="item" in my iterator in jsp, it doesn not use value from propertyList but from session !
My question is it is correct behaviour ?
This is defined behavior; whether or not it's "correct" is debatable.
Because you're using JSP EL, the Struts request wrapper is responsible for resolving JSP EL expressions. The normal application scopes are searched first (e.g., application, session, request). If nothing is found, only then will the value stack be queried for matching expressions.
If you accessed item via non-JSP EL means, e.g., the <s:property> tag, only the value stack would be queried, and you'd get the expected behavior.
When you mix ELs results are not always what you'd expect, so you must be aware how the frameworks in question relate to each other.

Creating a capitalize Method in Dart

I am trying to create a method in dart but have run into a wall. I was looking at how .toUpperCase(); and .toLowerCase(); were done. The method that I am trying to create is .capitalize();
I would like to call this method like this
String hello = "WORLD".capitalize(); //World
Here is the code I have so far
String capitalize() {
return this.codeUnitAt(0).toUpperCase() + this.substring(1).toLowerCase();
}
When running String hello = "WORLD".capitalize(); I get the following error
[38;5;124m[2015-6-4 11:37:13.011] Class 'String' has no instance method 'capitalize'.
NoSuchMethodError: method not found: 'capitalize'
Receiver: "WORLD"
Arguments: [][0m
I know i can call a function like String capitalize(String s) => s[0].toUpperCase() + s.substring(1);
But would much rather keep string Manipulation calls the same.
Thanks and I appreciate any help:)
You cannot extend the String class like you want. Just use it like this:
capitalize("WORLD");
Yes, not like JS, you can't just change any class in Dart. You can extend it only. E.g. you can create MyString class with capitalize method. But I don't think you want it. Just make some StringUtils.dart library with method capitalize
Since dart is more common now, there are a lot more packages available at pub.dartlang.org.
I found a nice dart package for different operation on strings. It also contains a capitalize method.
https://pub.dartlang.org/packages/basic_utils
Simply add the dependency :
dependencies:
basic_utils: ^1.0.3
Usage :
StringUtils.capitalize("helloworld"); // helloworld => Helloworld
It also contains other usefull methods :
camelCaseToUpperUnderscore
camelCaseToLowerUnderscore
isAscii
isNullOrEmpty ...
if this is still a problem, you can simple use this dependecy:
dependencies:
text_tools: ^0.0.2
Is simple to use, here is an example:
//This will put the first letter in UpperCase, will print 'Name'
print(TextTools.toUppercaseFirstLetter(text: 'name'));

Default stringify for objects, equivalent to Java's toString?

I have just take a look at the 3ยบ tutorial from dart, creating the rating component. I was wondering if there is same method which is called when stringifying an object, something similar to Java's toString.
For example:
MyClass myObject = new MyClass();
System.out.println(myObject);
Will call MyClass.toString() if overwriten, else will call it's parent until java.lang.Object is reached giving a default toString.
I find kind ugly (completely subjective) doing:
<span ng-repeat="star in cmp.stars" > {{star.toString()}} </span>
I would rather do:
<span ng-repeat="star in cmp.stars" > {{star}} </span>
And give the implementation of how I want it to display at an averwritten method. Is this possible?
If you have something like this:
class MyClass {
String data;
MyClass(this.data);
#override
String toString() {
return data;
}
}
MyClass myObject = new MyClass("someData");
print(myObject); // outputs "someData", not 'Instance of MyClass'
I think this might be what you are looking for.
Yes it works like this for print, String interpolation or Angular mustaches.
By overriding the String toString() method on your object the displayed value will be the result of this toString() call. If there's no toString() defined in the class hierarchy the toString() of Object will be called (which will return Instance of 'MyClass' for class MyClass{}).
You may be interesting look how Rating component was implemented in Angular Dart UI project. Check this out.
Sergey.

Resources