Struts2: Problems getting data from HTML/JSP - java

I have my action, which has a variable HashMap<String, MyObject>
My Object:
public class MyObject {
private Boolean confermata;
private String idObj;
private String versione;
/* (getters and setters) */
}
When JSP snippet:
<s:hidden name="form.datiVersioneQuoteAssegnazione['%{#tmpIdObj}'].confermata"/>
<s:hidden name="form.datiVersioneQuoteAssegnazione['%{#tmpIdObj}'].idObj"/>
<s:hidden name="form.datiVersioneQuoteAssegnazione['%{#tmpIdObj}'].versione"/>
tmpIdObj is another variable...it's fine.
Problem:
When I populate MyObject from the DB and I load the JSP the output is correct, but when I send the data to the server (clicking on a button in my <s:form>) the hashmap is built correctly. By debugging it, it is a <String,MyObject> but the values from the form are not taken, so the MyObjects objects are all empty... Furhtermore, I've seen that the setters of MyObjects are not called. Could somebody tell me why?

I solved it. The problem was that i defined a Hashmap:
private HashMap<String, MyObject> datiVersioneQuoteAssegnazione;
When i define it as a Map as following, it works:
private Map<String, MyObject> datiVersioneQuoteAssegnazione;
It seems that Struts doesn't recognize HashMaps...strange.

Related

How to create Excel report with subtable with Jasper reports

I am using Jasper reports to create Excel file programmatically. I am using Java Data Source - a java class that implements JRDataSource interface. Up until now My Datasource returned a List of class instances that looked something like this:
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
...
// getters and setters omitted to save space
}
With this Data Source I was able to create a very nice excel table that looked:
prop1-Header prop2-Header prop3-Header...
----------------------------------------
prop1-value prop2-value prop3-value...
prop1-value prop2-value prop3-value...
...
But now MyDataSource class has additional property List<String>
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
private List<String> subvalues;
...
// getters and setters omitted to save space
}
So I need my excel to look like this
prop1-Header prop2-Header prop3-Header...
----------------------------------------
prop1-value prop2-value prop3-value...
Sub-header1 Sub-header2...
-----------------------
sub-value1 sub-value2....
....
prop1-value prop2-value prop3-value...
Sub-header1 Sub-header2...
-----------------------
sub-value1 sub-value2....
....
...
I managed to do that by concatenating The list into a single string, and it looks very similar to what I need. But I have no way of sorting and filtering on sub-value data. So, I need to actually make it as sub-list or sub-table. And this is my question - how to do this?
After searching for a while I found the solution. First of all, the is a very nice youtube video
How to fill Jasper Report Table using Collection of data in Java?
This video shows in greate detail how to pass from java code into report an additional collection besides your Datasource java class. This is done by doing something like this:
JRBeanCollectionDataSource detailBean = new JRBeanCollectionDataSource(getMySubvaluesINstancesList());
Map<String, Object> params = new HashMap<>();
params.put("DetailDataSource", detailBean);
jasperPrint = JasperFillManager.fillReport(jasperReport, params, ds);
Note the name "DetailDataSource". In your report, you will need to create a parameter with that name and declare it's type as net.sf.jasperreports.engine.data.JRBeanCollectionDataSource. After that, you build a table based on a dataset that you create based on that parameter. But this is a short description of the video referenced above.
However, that alone doesn't solve the problem yet. The remaining problem that collection passed as a parameter will only be rendered once and will not be rendered for each record of your datasource. So, what we need to do is to add our list property into our datasource class, the same way as described in the question:
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
private List<String> subvalues;
...
// getters and setters omitted to save space
}
After that in your report you will need to declare a field named "subvalues" and declare its type as java.util.List. And then change the definition of your Dataset that you created following the video referenced above. You will need to change JRDatasource expression from $P{yourParameterName} to new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subvalues}) And bingo! your collection is part of a datasource and will be rendered for each record

Dozer map a List inside a Map

I'm using Dozer to Map from one object to another. I know Dozer can do a recursive mapping, but maybe i'm putting too much pressure on Dozer :p I want to map from class A to B
class A {
private Map<String, List<ObjectA>> myMap;
// getters and setter for myMap
}
class B{
private Map<String, List<ObjectB>> myMap;
// getters and setter for myMap
}
When I map from A to B I get an instance of B, but inside the Map I got a List of ObjectA. To be clear, I get an instance of this (imaginary) object:
class B{
Map<String, List<ObjectA>> myMap;
}
How can I make dozer to perform this mapping correctly?
Note: ObjectA and ObjectB have the same properties (and int and a String).
You could specified hint:
<field>
<a>A</a>
<b>B</b>
<a-hint>ObjectA</a-hint>
<b-hint>ObjectB</b-hint>
</field>

Conditional object population

Hy guys, I've turning around a problem using Struts2.
Basically I've a form that the user can submit filling different fields. I would like to populate conditionally the right object with the properties insert by the user.
So for example, if the user fill the form in a intermediary way I would like to use the properties for to create an Intermediary object, instead if the user fill the request in entity way, i'll use the properties for to create the Entity object, ecc...
These object share the same interface Request.
Is there any way to use polimorphically properties for to create Intermediary or Entity object?
Suppose this is my two POJO:
public class Intermediary implements Request{
private String name;
private String surname;
private String code;
private String FiscalCode;
private String address;
...
/*Getters and setters */
....
}
public class Entity implements Request{
private String name;
private String surname;
private String code;
....
/*Getters and setters */
....
}
This could be my form into the jsp page:
<s:form action="fillRequest" id="formRichiesta" name="formRichiesta" method="post">
<s:radio id="tipoRichiedente" name="tipoRichiedente" list="richiedenti">
<label>Name</label><s:textfield name="name"/>
<label>Surname</label><s:textfield name="surname"/>
<label>code</label><s:textfield name="code"/>
<label>Adress</label><s:textfield name="adress"/>
<label>Fiscal Code</label><s:textfield name="fiscalCode"/>
</s:form>
<s:submit>
Basically my problem is that I can discover the kind of request the the user trying to fill only ofter submission and watching at the radiobutton state.
Is there any way to mapping the properties into the right object directly instead of creating one big object with all the form properties and then create the right implementation? for to map properties could I use the interface reference?
For example image my action:
public class RequestAction {
private Request request;
....
}
So in my jsp could I use:
<label>Name</label><s:textfield name="request.name"/>
Thanks in advance.

Getting results for questionnaire JSF

I have just one idea as realize getting user's answers. I decide use List<String> questionsWithAnswers and put in this list pairs like '1_1', '1_2', '1_3', '2_1'. Something like this. But even with this idea I have a problem. And also important I'll use web-service (it's a constraint for using map for example as structure for saving pairs question and answer).
******** My bean ********
public class TestBean implements Serializable{
private static final long serialVersionUID = 1L;
private List<Test> testList;
private List<Answer> answerList;
private List<Subject> subjectList;
private Map<Question, List<QuestionAnswer>> mapQestionsWithAnswers;
private List<String> questionAnswerList;
private Long subjectId = 0L;
private Test test;
//...
}
getTest.xhtml
<c:forEach items="#{test.testBean.mapQestionsWithAnswers}"
var="entry">
<h:inputHidden value="#{entry.key.questionId}" />
<h:outputText value="#{entry.key.question}" rendered="#{not empty entry.value}"/>
<h:selectManyCheckbox value="#{test.testBean.questionAnswerList}" layout="pageDirection">
<f:selectItems value="#{entry.value}" var="ans"
itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
itemLabel="${ans.answer.answer}" />
</h:selectManyCheckbox>
</c:forEach>
<h:commandButton value="#{msgs['page.content.passtest']}" action="#{test.passTest}" />
For concatination questionId with answerId I use concat. I find this in Concatenate strings in JSF/JSP EL and Javascript
But I cant get value entry.key.questionId inside itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
I don't understand why.
Where can I make a mistake? And I want to ask some source on more logic and simple decision similar problem. Thanks
You don't need to concatenate :
itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"
could be as simple as :
itemValue="#{ans.answer.answerId}, #{entry.key.questionId}"
Why not using your own method to concatenate? You can use a bean named concatenatorBean having method
String customConcat(String str1, String str2) {
return str1+"_"+str2;
}
Then in getTest.xhtml you can use your own concatenator to solve the problem.

list of string not displayed

I use spring 3. I'm trying to display a value of an object in a jsp.
public class UserForm implements Serializable {
private List<String> values;
private String date;
...
}
Here is my controller:
#Controller
#RequestMapping("/user.htm")
public class UserController {
#Autowired
private IUserService userService;
#RequestMapping(method = RequestMethod.GET)
public String userStat(Model model) {
UserForm stat = userService.populateStatistique();
stat.setDate("today");
model.addAttribute("statistiqueForm", stat);
return "userStat";
}
}
Here is my jsp:
<form:form commandName="userForm" name="userForm">
Date: <form:input path="date"></form:input>
<br/>
Expediteur:
<form:select path="values" items="${values}" />
<br/>
<input type="submit" value="create" />
</form:form>
In the jsp I can see the today value for the date field, but the listbox is empty.
any idea?
thanks
Well, use addAttribute("values", list) if you want it accessible in the jsp. You are currently not setting it and so it is empty.
If that list is contained in the statistiqueForm object, then use items="${statistiqueForm.values}".
You're correctly passing the form object to the jsp page. In that page you have a list box, which has a list of potential values and a list of selected values. The object form contains the list of selected values, but you should also setup the list with all potential values. In fact you reference ${values} in the jsp, but you don't pass it to the jsp. You should add this code to your controller:
model.addAttribute("values", potentialValueList);
I also suggest you to change the name of that list to avoid confusion, so it will be easy to understand the difference from selected values to potential values.

Categories

Resources