Getting results for questionnaire JSF - java

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.

Related

How to findAll entires(posts) which contain at least one specified tag. Spring Data

I`m trying to retrieve only entries(posts) which possess a specific tag. One entry can have many tags, so I use List of objects. But I don't know how to construct a proper command in Controller class, actually I'm afraid I’m completely lost here.
Entry entity looks like this:
#Entity
public class BlogEntry {
#Id
#GeneratedValue
private Integer id;
private String title;
#Column(name = "published_date")
private Date publishedDate;
#ManyToMany
#JoinTable
private List<TagBlog> blogTags; /* Multiple tags to one entry */
And my Tag entity:
#Entity
public class TagBlog {
#Id
#GeneratedValue
private Integer id;
private String tag;
#ManyToMany(mappedBy="blogTags")
private List<BlogEntry> entries;
In my EntryService class I wanted to perform this kind of sort "findByTagBlogIn" which I wish would return List of posts that possess specific tag.
public List<BlogEntry> findAllByTags(List<TagBlog> tag){
List<BlogEntry> blogEntry = entryRepository.findByTagBlogIn(tag);
return blogEntry;
}
But I don't know how to refer to it in Controller class. How can I retrieve only entries with specific tag? Something like this? But how to pass List of tags as a parameter, maybe it should be String?
#RequestMapping(value="/welcome")
public String retrieveTaggedEntry(Model model, ?List of tags?){
model.addAttribute("entriesWithTag", entryService.findAllByTags(TagBlog tag));
return "redirect:/welcome.html";
}
In the welcome.jsp file I would like to iterate throught whole List of tags that had been assigned to specific entry(post) like in example below (between the arrows "---> <---" is the part of my conserns):
<c:forEach items="${entries}" var="entry"> <!--"entries" refers to List of BlogEntry-->
<table>
<tr>
<td>Entry No. ${entry.id }</td>
<td>${entry.title}</td>
<td>
Tags: ---> #${entry.? blogTag.tag ?}, <---
</td>
<td>Published: ${entry.publishedDate}</td>
<td>
<spring:url value="/delete_entry/${entry.id}.html" var="url" />
Delete
</td>
</tr>
</table>
</c:forEach>
By working it out later, I want to perform a sort (by mapping spring:url value="/tag/${some_tag_as_a_String}.html") by entries that possess a specific tag.
Maybe there is an easier way to return posts only with specific tag? But I guess it would be easier for me to work with what I got here. Anyway, any solution provided is appreciated.
I'm willing to add any additional information if needed.
Is there any reason you want to send the whole TagBlog object to the Controller? Why not just the tag ID or tag text? E.g.
#RequestMapping(value="/welcome")
public String retrieveTaggedEntry(Model model, #RequestParam List<String> tags) {
// Do something with your tags
}

pass form values in managed bean

i want to pass my form value into my managed bean in order to process it but i always got a null value when i try to retrieve the value in my action method
My bean
#ManagedBean(name="datas")
#SessionScoped
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
private String ID_primary;
public Datas(){
}
public Datas (String ID_primary){
this.ID_primary=ID_primary;
}
public String getID_primary() {
return ID_primary;
}
public void setID_primary(String ID_primary) {
this.ID_primary= ID_primary;
}
public void process() {
Map<String, String> request=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String value = request.get("ID_primary"); // always return null
System.out.println("ID" + value);
}
}
My form
<h:form>
<div align="left">
<h:inputText id="ID_primary" name="ID_primary" value="#{datas.ID_primary}" />
</div>
..........
<div align="right">
<h:commandButton action="#{datas.process()}" value="Create" type="submit" />
</div>
</h:form>
Thank you very much for your help
You have two different form elements, when you click commandButton in the bottom form, values from the top form are not send with the request.
You can have tags like div inside a form, so you can make on big form with divs inside if it is the reason why you have have split them.
Also add #ManagedProperty annotation to ID_primary field:
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value = "#{Datas.ID_primary}")
private String ID_primary;
[..]
EDIT:
Ok, my bad, I have not looked at the code carefully. In ManagedBean with ManagedProperty this property will be set automatically by JSF, so you can read it like this:
public void process() {
System.out.println("ID" + getID_primary());
}

Struts2: Problems getting data from HTML/JSP

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.

JSF primefaces showing list of object not list of string

I already tried to search through this site, but maybe I'm not getting understand well.
Please kindly advice for my cases..
I'm using Netbeans 7.3 + Primefaces + Hibernate.
I want to show a list from my query.
My query already in.. and there's no error showup, but the display is not what I want (I think it returning an object or something, not sure).
Please kindly correct me if I'm missed something.
Here's my PtlLovBean
#ManagedBean(name = "ptlLovBean")
#SessionScoped
public class PtlLovBean implements Serializable {
private static final String FLIGHT = "LOV_FLIGHT";
private List lovFlight;
public List getLovFlight() {
PtlLovDao ptlLovDao = new PtlLovDaoImpl();
return ptlLovDao.getByKey(FLIGHT);
}
}
Here's ptlLovDao
public interface PtlLovDao {
public List getByKey(String key);
}
Here's PtlLovDaoImpl
public class PtlLovDaoImpl implements PtlLovDao {
#Override
public List getByKey(String key) {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("from PtlLov where LOV_KEY = :param");
query.setParameter("param", key);
return query.list();
}
}
Here's my JSF :
<p:selectOneMenu id="flightName" value="#{wizard.user.selectedFlightName}">
<f:selectItem itemLabel="Select Flight" itemValue="" />
<f:selectItems value="#{ptlLovBean.lovFlight}" />
</p:selectOneMenu>
Display after Code:
Sorry I'm not capable to insert picture, so here's the image link :
http://i117.photobucket.com/albums/o56/po_se_for/PIC_zps88ec4983.png
You can either override the toString method in your PtlLov class or define itemValue and itemLabel properties in your f:selectItems tag.
Something like this:
<f:selectItems value="#{ptlLovBean.lovFlight}" var="flight"
itemValue="#{flight}" itemLabel="#{flight.description}" />

How can i get value from <h:selectManyCheckbox>

I have one collection , which I'm displaying in my .xhtml page by <h:dataTable>. I want to reach a few goals: at first I want to set list.check value by <h:selectManyCheckbox>, at second I want whatever the values remains selected until the end of current session. Now It displays correctly, but when I select some value it doesn't transmit it in list.check property. I'm using JSF v.2.2.
Code in JSF bean:
private List<AnswerDTO> answerListDto;
//getters and setters
Code in .xhtml
<h:form>
<h:dataTable value="#{main.answerListDto}" var="list">
<h:column>
<h:selectManyCheckbox value="#{list.check}">
<f:selectItem itemValue="1" itemLabel="#{list.ansValue}" />
</h:selectManyCheckbox>
</h:column>
</h:dataTable>
</h:form>
AnswerDTO class:
public class AnswerDTO implements Serializable, DTO {
private static final long serialVersionUID = 1L;
private Integer id;
private Question questId;
private String ansValue;
private String ansStatus;
private String check;
//getters and setters
}
Strange use of selectManyCheckbox I must say. Concrete problem is that value of selectManyCheckbox must be an array.
My opinion is that selectBooleanCheckbox should be used instead of selectManyCheckbox. Your check property can have only two values, an empty array (if checkbox is not selected), and array of length 1 with value which is equal to ansValue (if checkbox is selected).

Categories

Resources