Dynamic Textfields in Struts2 - java

All -
New to struts2 here.. I've been reading some tutorials, and even picked up a book so I understand some, but please forgive me if I use the wrong terms.
I'm looking for an example or an explanation on how to create a dynamic list of text fields based on a collection, have the user enter some input and then assign that input back to the object.
Example:
I have a dynamic set of products, I'd like to be able to display a table of product names and textfields where the user can enter a price for that product.
Product1 ....... [ textfield_price1 ]
Product2 ....... [ textfield_price2 ]
...
ProductN
[submit_button ]
How do I then map those textfield values back to each product, most of the examples I have seen are standard forms with static information.
Can anyone point me to the right place ?
Thanks in advance.

Typically you'd use map-based syntax, usually with an ID as the key, and a domain object as the value. This is covered in the type conversion docs.
Nutshell: the action has a map of { ID => Product } for the form data:
public Map<Long, Product> getProductMap() { ... }
// and setter, and private property
Use [] in the JSP with an ID as thename` attribute value:
<s:textfield name="productMap[%{id}]" ... />
You could iterate over a collection of products, a pre-filled map, etc.

Related

Push data from a dataframe to a Map where values are List of objects in Scala

I am working on a Scala codebase and I have to implement a scenario which uses some data structure to populate information for further processing.
The gist of the problem is,
I have a dataframe studentDf which has the student marksheet information eg.
Name, ID, Subject, Details, Marks, isFail
Now there can be multiple records for the same Name-ID mapping. I have to reflect the scenario where if the student has failed in any subject, the details (and the corresponding record) will pop up in a resultDf. And if he has not failed in any subject (congratulations!!!) then we can populate any record corresponding to the Name-ID mapping.
Basically what I would do in Java 8 for this is,
Assuming I have List<StudentMarks> studentMarksList => list of all the marks of all the students.
Map<String, List<StudentMarks>> studentToMarkMapping = new HashMap<>();
studentMarksList.stream().foreach(studentMark->{
studentToMarkMapping.computeIfAbsent(studentMark.getName()+"_"+studentMark.getID, k => new ArrayList<>()).add(studentMark);
}
Set<Student> resultSet = new HashSet<>();
for(Map.Entry<String,List<StudentMarks>> studentToMark : studentToMarkMapping){
List<StudentMarks> studentMarks = studentToMark.getValue();
for(StudentMarks studentMark : studentMarks){
if(studentMark.getFailed() == true){//Return corresponding failed subject record
resultSet.add(studentMark);
break;
}
}
resultSet.add(studentMark.get(0)); // just add any subjectMark for student who has passed all subjects
}
In Scala to do the same, I was trying to load the data into a Mutable Map, but to populate multiple records for the same student into a list and then find out whether he has failed in any or not, I am getting stuck. I see the concept of using ListBuffer which is a mutable variant of a list, but I am confused how to use it. It is possible that we can do without Map as well, but I tried some other ways which didn't end up working.
If somebody can provide any help on this, would be great. Thanks a lot!!!

how to get a search box using jhipster:import-jdl?

Lets say that we have a book and author entities in JDL-Studio like this:
entity Book {
name String required minlength(2)
}
entity Author {
name String required minlength(2)
}
relationship ManyToMany {
Book{author(name)} to Author{book}
}
When we run the show, we can login and create a new book, but we need to choose between the list of authors in the dropdown list that we have already created in the author entity, right?
How can we do this 2 things using jhipster:import-jdl:
1) tell Jhipster-JDL to create a search box to look in a list of authors (let's say we have a 1000 author entries) and
2) have a text box where we can add a new text creating a new author entry in the database (or think about a tag for the book: adventures, science-fiction,... but not using the already created from the list).
Is it posible to do that when creating de JDL-Studio Entities? Is there any place with more advanced use cases besides https://www.jhipster.tech/jdl/
Would it be possible with jhipster-uml? Is UML, more powerful? https://www.jhipster.tech/jhipster-uml/

Elasticsearch multiple fields autosuggestion

I want to implement autosuggestion functionality using elastic search. I can use nGram filters to match partial words on multiple fields and its working fine as expected. Output of the search returns full document with multiple fields as required. Now my problem is, how do I give autosuggestion to the user based on the matching field. e.g. I have got 5 fields:
{userId:'rakesh',firstName:'Rakesh','lastName':'Goyal','mobileNo':'123-123-1234','alternativeMobileNo':'123-123-1235'}
{userId:'goyal',firstName:'Goyal','lastName':'Rakshit','mobileNo':'123-123-1236','alternativeMobileNo':'123-123-1237'}
In the above example if user types 123, I want to return 123-123-1234, 123-123-1235, 123-123-1236, 123-123-1237 (4 auto suggestions).
Similarly if user types Rak, I want to return Rakesh, Rakshit (2 auto suggestions).
How do I know match exists in mobileNo and alternativeMobileNo field for first example and return results accordingly?
How do I know match exists in firstName and lastName field for second example and return results accordingly?
How do I give autosuggestion to the user based on the matching field?
When user types 123, store it in a Java variable, prepare a query like below inserting that variable into and send a request to ElasticSearch.
{
"query" : {
"query_string" : {
"query" : "*123*"
}
}
}
The above query will manage to check it in both fields mobileNo and alternativeMobileNo.
Similarly, if user types Rak, the query will be similar to the previous one,
{
"query" : {
"query_string" : {
"query" : "*Rak*"
}
}
}
And I think you want to use highlighter api to answer your last how questions, which allows to highlight search results on one or more fields.
A screenshot of highlight example in es :

how can I get dynamic property in drools

I have a XML file containing metadata like a field's maximum length. I have to use drools to build rules to validate this metadata against a list of facts. I don't want to hardcode the name of each field that may or may not be specified in the XML.
I tried to do this :
when
$metadata: Metadata(maxLength != null);
$obj: Object(eval($metadata.getFieldName()).length > $metadata.maxLength);
then
// TODO
end
It does not work and I get the following error :
java.lang.IllegalStateException: Errors while building rules : Unable
to Analyse Expression $metadata.getFieldName() > $metadata.maxLength:
[Error: Comparison operation requires compatible types. Found class
java.lang.String and class java.lang.Integer] [Near : {...
$metadata.getFieldName() > $metadata.maxLength ....}]
Is it possible to dynamically get a field name and compare its maximum length? Will I have to create a java object to accomplish this?
Thank you
You talk of XML and metadata. Can you distinguish all entities? For example, if it is about orders, can you extract each order, and attributes of each order?
I solved a similar problem with using maps to store each attribute.
public class Order{
private int id;
private Map<String, Integer> num_attribute_map = new HashMap<String, Integer>();
public Map getNumAttributeMap(){
return this.num_attribute_map;
}
If an order has customer_satisfaction = 5,
order_obj.getNumAttributeMap().put("customer_satisfaction" , 5);
And thus you have created Orders with their attributes stored in the numAttributeMap.
For implementing a rule on an Order
$ord : Order(
getNumAttributeMap[$attribute] >= $value
)
where $attribute would be "customer_satisfaction", of course. The [] notation is used to access elements of a list, given index or values of a map, given the key.
Hope you "get" the concepts of maps. Also, do look up Drools language support for list and map access.
I have also implemented maps of lists of strings to perform an "is in" operation, in addition to maps of integers that do comparison operations. Please refer https://stackoverflow.com/a/9241089/604511 too
Finally, I have decided to generate my drools file dynamically from my XML using rule templates.

Javascript Confirm Message on List on the server

I have a List of employees on the backing bean , I want to loop on this list and display a javascript confirm message(yes/no) for each employee..
assume the employee class has id , name , salary attributes
I want to display confirmation message
(Are you sure to save employee with id= , name= ,salary =) for each employee..
I need to know how to do this
It is not an actual use case that is a simple assumption because the actual one is complex to illustrate , just I need to Know the concept to do this..
Thanks In Advance
How about something similar to this:
for (i=0;i<employeeList.size;i++)
{
alert([insert your data here]);
}

Categories

Resources