I want to create editable h:panelGrid. I created this ArrayList:
public ArrayList<userdata> dataList = new ArrayList<>();
public class userdata
{
int userid;
int groupid;
String specialnumber;
String username;
String passwd;
Date datetochangepasswd;
String address;
String stateregion;
String country;
String userstatus;
String telephone;
Date dateuseradded;
Date userexpiredate;
Date dateuserlocked;
String city;
String email;
String description;
public userdata(int userid, int groupid, String specialnumber, String username, String passwd, Date datetochangepasswd,
String address, String stateregion, String country, String userstatus, String telephone, Date dateuseradded,
Date userexpiredate, Date dateuserlocked, String city, String email, String description)
{
this.userid = userid;
this.groupid = groupid;
this.specialnumber = specialnumber;
this.username = username;
this.passwd = passwd;
this.datetochangepasswd = datetochangepasswd;
this.address = address;
this.stateregion = stateregion;
this.country = country;
this.userstatus = userstatus;
this.telephone = telephone;
this.dateuseradded = dateuseradded;
this.userexpiredate = userexpiredate;
this.dateuserlocked = dateuserlocked;
this.city = city;
this.email = email;
this.description = description;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
public Date getDatetochangepasswd()
{
return datetochangepasswd;
}
public void setDatetochangepasswd(Date datetochangepasswd)
{
this.datetochangepasswd = datetochangepasswd;
}
public Date getDateuseradded()
{
return dateuseradded;
}
public void setDateuseradded(Date dateuseradded)
{
this.dateuseradded = dateuseradded;
}
public Date getDateuserlocked()
{
return dateuserlocked;
}
public void setDateuserlocked(Date dateuserlocked)
{
this.dateuserlocked = dateuserlocked;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getGroupid()
{
return groupid;
}
public void setGroupid(int groupid)
{
this.groupid = groupid;
}
public String getPasswd()
{
return passwd;
}
public void setPasswd(String passwd)
{
this.passwd = passwd;
}
public String getSpecialnumber()
{
return specialnumber;
}
public void setSpecialnumber(String specialnumber)
{
this.specialnumber = specialnumber;
}
public String getStateregion()
{
return stateregion;
}
public void setStateregion(String stateregion)
{
this.stateregion = stateregion;
}
public String getTelephone()
{
return telephone;
}
public void setTelephone(String telephone)
{
this.telephone = telephone;
}
public Date getUserexpiredate()
{
return userexpiredate;
}
public void setUserexpiredate(Date userexpiredate)
{
this.userexpiredate = userexpiredate;
}
public int getUserid()
{
return userid;
}
public void setUserid(int userid)
{
this.userid = userid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getUserstatus()
{
return userstatus;
}
public void setUserstatus(String userstatus)
{
this.userstatus = userstatus;
}
}
// Getter for the data list
public ArrayList<userdata> getuserdata(){
return dataList;
}
I want to display the data into h:panelGrid. Can you tell me how I can do this?
I want to create this editable table but with h:panelGrid. I suppose that it's possible to use h:panelGrid instead h:dataTable?
Best wishes
While it may be actually possible to hack something with h:panelGrid, you probably would have to fiddle with a lot of unnecessary code in your MBs, just for creating and binding components.
If you want a finer grained control for your HTML table, what you want to do is probably to use ui:repeat, something like this:
<table>
<ui:repeat var="elem" value="#{yourMB.yourDataList}">
<tr>
<td>#{elem.userid}</td>
<td>
<h:outputText value="#{elem.name}"
rendered="#{not elem.editable}" />
<h:inputText value="#{elem.name}" rendered="#{elem.editable}" />
</td>
<td>
<h:outputText value="#{elem.telephone}"
rendered="#{not elem.editable}" />
<h:inputText value="#{elem.telephone}"
rendered="#{elem.editable}" />
</td>
<td>
<h:commandLink value="Edit" rendered="#{not elem.editable}"
action="#{yourMB.editAction(elem)}" />
</td>
</tr>
</ui:repeat>
</table>
<h:commandButton value="Save Changes" action="#{yourMB.saveAction}" />
For this to work you should have a backing bean looking like this:
#ManagedBean
#ViewScoped
public class YourMB {
private List<Elem> dataList;
#PostConstruct
public void init() {
// initialize dataList here
}
public void editAction(Elem e) {
e.setEditable(true);
}
public void saveAction() {
// do your thing here to update in the database,
// and then reset the editable property for all items:
for (Elem e : dataList) {
e.setEditable(false);
}
}
// getters and setters...
}
Check out the tutorials available here, which teaches how to use ui:repeat and also presents other options for looping in JSF.
Related
Situation: I'm trying to get all persons as a rest service. But the problem is that the class Person has persons on his own(Friends). So I'm getting an infinite long json, cause of the friends connection ...
The result were I'm aiming for:
`{"naam":"Lance","voornaam":"ADAM","email":"Adam.Lance#msn.com","password":"fxSg1vSa2zzqHxuTCrDSbtp9ITlHf9ALSnS/ENFXfAA=$7vSVqz5nHZ7cEA4u6OiTBDw+CGaOJkhun4YuievZCKc=","username":"adam","status":"online","posts":[{"message":"Ik heb mijn eerste auto gekocht.","mood":"trots","when":1494603638834},"role":"ADMIN","friends":[{"username":"adamsFriend1"},{"username":"adamsFriend2"}]
So that I'm only getting the usernames of those friends. if this is not possible is there a way to exceclude those friends?
The Restcontroller:
#RestController
#RequestMapping(value = "/users")
public class UsersRestController {
private final Service service;
public UsersRestController(#Autowired Service service) {
this.service = service;
}
#RequestMapping(method = RequestMethod.GET)
public List<Person> getUsers() {
return service.getAllPersons();
}
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Person getUser(#PathVariable String id) {
return service.getPerson(id);
}
}
the Person class:
#Entity
public class Person {
private String naam, voornaam, email, password;
#Id
private String username;
private String status;
#ManyToMany
private Collection<Person> vrienden;
#OneToMany(orphanRemoval = true)
private Collection<Post> posts;
#Enumerated(EnumType.STRING)
private Role role;
public Person(String naam, String voornaam, String email, String password, String username) {
setNaam(naam);
setEmail(email);
setStatus("online");
setVoornaam(voornaam);
setPassword(password);
setUsername(username);
vrienden = new HashSet<>();
posts = new HashSet<>();
role = Role.USER;
}
public static void addFriend(Person a, Person b) {
a.addFriend(b);
b.addFriend(a);
}
private void addFriend(Person b) {
this.vrienden.add(b);
}
public static void deleteFriend(Person a, Person b) {
a.deleteFriend(b);
b.deleteFriend(a);
}
private void deleteFriend(Person a) {
this.vrienden.remove(a);
}
public Collection<Person> getFriends() {
return vrienden;
}
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
#NotNull(message = "{error.no.name}")
#Size(min = 2, message = "{error.invalid.namesize}")
public String getNaam() {
return naam;
}
public void setNaam(String naam) {
this.naam = naam;
}
#NotNull(message = "{error.no.surnaam}")
#Size(min = 2, message = "{error.invalid.surnamesize}")
public String getVoornaam() {
return voornaam;
}
public void setVoornaam(String voornaam) {
this.voornaam = voornaam;
}
#NotNull(message = "{error.no.email}")
#Email(message = "{error.invalid.email}")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#NotNull(message = "{error.no.status}")
#Size(min = 1, message = "{error.no.valid.status}")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#NotNull(message = "{error.no.username}")
#Size(min = 2, message = "{error.invalid.usernamesize}")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = Password.getSaltedHash(password);
}
public boolean isPasswordCorrect(String password) {
boolean result = false;
result = Password.check(password, this.password);
return result;
}
#NotNull(message = "{error.no.password}")
#Size(min = 2, message = "{error.invalid.usernamesize}")
public String getPassword() {
return this.password;
}
public void addPost(Post p) {
if (p == null) {
throw new DomainException("Post is null");
}
posts.add(p);
}
public void deletePost(Post p) {
if (p == null) {
throw new DomainException("Post is null");
}
posts.remove(p);
}
public Collection<Post> getPosts() {
return posts;
}
#Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + Objects.hashCode(this.username);
return hash;
}
public void setHashedPassword(String password) {
this.password = password;
}
}
just made a wrapper class for Person. There is probably a better way... .
but if you intrested this is the wrapper class:
public class WrapPerson {
private String naam;
private String voornaam;
private String email;
private String username;
private String status;
private Collection<String> vrienden;
private Collection<Post> posts;
public WrapPerson(Person person) {
setEmail(person.getEmail());
setNaam(person.getNaam());
setPosts(person.getPosts());
setStatus(person.getStatus());
setUsername(person.getUsername());
setVoornaam(person.getVoornaam());
this.vrienden = new ArrayList<>();
for (Person p : person.getFriends()) {
this.vrienden.add(p.getUsername());
}
}
public String getNaam() {
return naam;
}
public void setNaam(String naam) {
this.naam = naam;
}
public String getVoornaam() {
return voornaam;
}
public void setVoornaam(String voornaam) {
this.voornaam = voornaam;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Collection<String> getVrienden() {
return vrienden;
}
public void setVrienden(Collection<String> vrienden) {
this.vrienden = vrienden;
}
public Collection<Post> getPosts() {
return posts;
}
public void setPosts(Collection<Post> posts) {
this.posts = posts;
}
}
`
I am doing a project on project managment, so I have to show the project details of user once logged in. I'm taking down all the project details in a list so that after login validation, I can get to the project details. But don't know how to call that and plz can someone help me figure out how to store the ID in
//controller
public static Result login() {
User user = Form.form(User.class).bindFromRequest().get();
CommonService service = new CommonServiceImpl();
boolean response=service.login(user);
if(response){
return ok(welcome.render());
}
else{
return ok("not sucees");
}
}
how to call another method to show project details
//daoimpl
public boolean login(User user)
{
try {
con = JdbcUtil.getSqlConnection();
VERIFY_USER=select * from project_managment.t_user where email=? and password=?;
ps = con.prepareStatement(VERIFY_USER);
ps.setString(1, user.getEmail());
ps.setString(2, user.getPassword());
rs = ps.executeQuery();
if(rs.next()){
response=true;
}
}
catch (Exception e) {
}
finally {
JdbcUtil.dbResourceCleanUp(rs,ps,con);
}
return response;
}
how to call another method to show project details
//user is my model class
public class User {
int id;
String firstname;
String lastname;
String password;
String email;
int phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
//projectdetails modelclass
public class ProjectDetails {
String ProjectName;
String StartDate;
String endDate;
int hours;
String mngrName;
public String getProjectName() {
return ProjectName;
}
public void setProjectName(String projectName) {
ProjectName = projectName;
}
public String getStartDate() {
return StartDate;
}
public void setStartDate(String startDate) {
StartDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public String getMngrName() {
return mngrName;
}
public void setMngrName(String mngrName) {
this.mngrName = mngrName;
}
}
To get your answer have a look at ACTIVITY and ACTIVITY LIFECYCLE.
Once you are done with login task, you can finish that activity and start new activity having list.
In JSP in drop down box I have a list of countries. Selected country I should assign to "Organization" object as an object "Country".
JSP:
<select name="country">
<c:forEach var="country" items="${countries}" >
<option value="${country}">
<c:out value="${country.name}" />
</option>
</c:forEach>
</select>
Model, Organisation:
#Entity
public class Organization {
#Id
#GeneratedValue
private Integer id;
private String name;
//I should assign "Country" object to this field
#OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
#JoinColumn(name="country")
private Country country;
private String address;
private String phone;
private Long market_cap;
public Organization(Integer id, String name, Country country, String address, String phone, Long market_cap) {
this.id = id;
this.name = name;
this.country = country;
this.address = address;
this.phone = phone;
this.market_cap = market_cap;
}
public Organization() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Long getMarket_cap() {
return market_cap;
}
public void setMarketCap(Long market_cap) {
this.market_cap = market_cap;
}
}
Model, Country:
#Entity
public class Country {
#Id
#GeneratedValue
private Integer id_country;
private String name;
private String isocode;
public Country() {
}
public Country(Integer id_country, String name, String isocode) {
this.id_country = id_country;
this.name = name;
this.isocode = isocode;
}
public Integer getId_country() {
return id_country;
}
public void setId_country(Integer id_country) {
this.id_country = id_country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsocode() {
return isocode;
}
public void setIsocode(String isocode) {
this.isocode = isocode;
}
}
Controller method:
#RequestMapping(value="add", method=RequestMethod.GET)
public ModelAndView addOrganization() {
ModelAndView modelAndView = new ModelAndView("add");
Organization organization = new Organization();
modelAndView.addObject("organization", organization);
List<Country> countries = countryService.listOfCountries();
modelAndView.addObject("countries", countries);
return modelAndView;
}
I have Bad Request error.
I pass couple of objects "Studentdetails" and String-"dept" to JSP.
Question-
How will I refer studentdetail.st.fname in my JSP file? ie; need to refer the firstname from the Student object.
I tried this and its failing -
<form:label path="s.studentdetail.st.fname" class="labels">First Name</form:label>
AND
<form:label path="studentdetail.st.fname" class="labels">First Name</form:label>
#RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("studentdetail", new Studentdetails());
model.put("department", "dept");
return new ModelAndView("student", "s", model);
}
Studentdetails:
public class Studentdetails {
public ContactInfo getCi() {
return ci;
}
public void setCi(ContactInfo ci) {
this.ci = ci;
}
public Student getSt() {
return st;
}
public void setSt(Student st) {
this.st = st;
}
ContactInfo ci;
Student st;
}
Student:
public class Student {
private Integer age;
private String fname;
private String mname;
private String lname;
private String dob;
private String gender;
private String birthplace;
private String nationality;
private String mothertongue;
private String religion;
private Integer id;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthplace() {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public String getMothertongue() {
return mothertongue;
}
public void setMothertongue(String mothertongue) {
this.mothertongue = mothertongue;
}
public String getReligion() {
return religion;
}
public void setReligion(String religion) {
this.religion = religion;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
COntactinfo
public class ContactInfo {
String addr1;
String addr2;
String city;
String state;
String pin;
String country;
String phone;
String mobile;
String email;
public String getAddr1() {
return addr1;
}
public void setAddr1(String addr1) {
this.addr1 = addr1;
}
public String getAddr2() {
return addr2;
}
public void setAddr2(String addr2) {
this.addr2 = addr2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Working code:---
<h2>Student Information</h2>
<form:form method="POST" action="/SpringMVC/addStudent" modelAttribute="studentdetail">
<br><br>
<form:label path="st.fname" class="labels">First Name</form:label>
<form:input path="st.fname" class="textbox" />
<br><br>
studentdetail:${studentdetail.st.fname}
<br>
Department:${department}
<br><br>
</form>
Controller:-
#RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("studentdetail", new Studentdetails());
model.put("department", "dept");
return new ModelAndView("student",model);
Try :
<form:label path="s['studentdetail'].st.fname" class="labels">First Name</form:label>
If you are using an array of Studentdetails then you have to use that array on your form declaration on the modelAndAttribute field, and then using a foreach to iterate everyone, and using the index.
<form:form
id=""
method="post"
action=""
modelAttribute="studentdetail">
<form:input type="text" id=""
path="studentdetail.st.fname"/>
</form:form>
First your construction of a ModelAndView is at least uncommon. You add an unnecessary level of indirection by putting in string model an only attribute s that contains the real model. In JSP you should have to use ${s["studentdetail"].st.fname} ... if you had initialized studentdetail.st ...
IMHO, you'd better change your method to :
public ModelAndView student() {
Map<String, Object> model = new HashMap<String, Object>();
Studentdetails studentdetail = new Studentdetails());
studentdetail = new Studentdetails());
studentdetail.setSt(new Student()); //put a Student in studentdetail
model.put("studentdetail", studentdetail);
model.put("department", "dept");
return new ModelAndView("student", model); // directly use model in ModelAndView
}
Then in the JSP, you simply use ${studentdetail.st.fname}
I am developing an Online Book Library application in struts...
I have a user form wherein user will enter his details like first name,last name etc and also there would be a books list where he will select some books that he wants..an d later on I want to insert those details into 2 tables..i.e user details in obl_users and and books that user select in users_books.
I used the below code for the list ..
<%
if(request.getAttribute("booksNameList") != null) {
%>
<html:select property="displayBooks" multiple="true" size="5">
<logic:iterate id="booksNameList" name="booksNameList" scope="request">
<html:option value="${booksNameList.bookId}" ><bean:write name="booksNameList" property="longTitle" /></html:option>
</logic:iterate>
</html:select>
<%
}
%>
Initially when user request form then the form will have pre populated list will all the books name in database..I am not sure about the code that i wrote to get book id in the value part of i.e value="${booksNameList.bookId}" ..
In my addUser() I want to iterate through the user selected books like this..
for (int i = 0; i < selectedBooks.length; i++) {
//insertBooks.setInt(1, generatedKeys.getInt(1));
//insertBooks.setInt(2, Integer.parseInt(selected[i]));
//insertBooks.addBatch();
}
but for that how to get the user selected books..
Here is my user.java
public class User extends ActionForm {
private int userId;
private String firstName;
private String lastName;
private String middleName;
private String username;
private String password;
private String contactNumber;
private String membershipNumber;
private String role;
private String email;
private String address;
private String comments;
private String dateOfBirth;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayBooks;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getMembershipNumber() {
return membershipNumber;
}
public void setMembershipNumber(String membershipNumber) {
this.membershipNumber = membershipNumber;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getOblStatus() {
return oblStatus;
}
public void setOblStatus(int oblStatus) {
this.oblStatus = oblStatus;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public String getDisplayBooks() {
return displayBooks;
}
public void setDisplayBooks(String displayBooks) {
this.displayBooks = displayBooks;
}
}
book bean class:
private String bookId;
private String longTitle;
private String shortTitle;
private String isbn;
private String dateOfPublication;
private String noOfPages;
private String boundType;
private String dvdAvailability;
private String noOfAvailableCopies;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayAuthors;
private int[] authorIds;
please guide me...i am totally new to struts
You have to put "displayBooks" property as a String array like;
private String[] displayBooks;
Since you want to select multiple values on the form, Array or ArrayList can be the only better option to use.