Currently, I want to get list of selected checkbox from a table.
and I tried to have a sample code as below :
public class Student {
public List<String> listSubject;
public List<String> getListSubject() {
return listSubject;
}
public void setListSubject(List<String> listSubject) {
this.listSubject = listSubject;
}
private int id;
private String name;
private int age;
public boolean single;
public boolean isSingle() {
return single;
}
public void setSingle(boolean single) {
this.single = single;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(List<String> listSubject, int id, String name, int age,
boolean single) {
super();
this.listSubject = listSubject;
this.id = id;
this.name = name;
this.age = age;
this.single = single;
}
}
And the blow is controller
and StudentForm to add information
after selected checkbox from form, I want to display all result to a view :
But until now, I still can't controller adding selected value into a listofSubject which I created for a student.
THe blow is the link of sample code which I am implementing :
https://dl.dropboxusercontent.com/u/11576807/spring-mvc-example.zip
Besides, I want to use a tag instead of submit button to redirect to result page.
And the system only allow user to select two options, at that time, the remain checkbox will be disabled.
Can you please share with me your solution in this case ?
Please tell me know the way to do it with the sample above.
Thanks
You already found the answer. Check stu.getListSubject(). All the checked items will populated to List by Spring MVC.
Your controller should look like this.
#RequestMapping(value = "/student/add", method = RequestMethod.POST)
public String addStudent(Student stu, ModelMap model){
for (String s: stu.getListSubject()) {
//You can see values populated
System.out.println("string: " + s);
}
model.addAttribute("name",stu.getName());
model.addAttribute("age", stu.getAge());
model.addAttribute("single", stu.isSingle());
model.addAttribute("listSubject", stu.getListSubject());
return "studentView";
}
And you have error in your studentView.jsp file.
Instead of this
<c:forEach items="listSubject" var="subject">
<td>${subject}</td>
</c:forEach>
use this:
<c:forEach items="${listSubject}" var="subject">
<td>${subject}</td>
</c:forEach>
You missed ${} .
Related
I need to create a new multiple instance of objects for the Pojo class in drools decision table. I have implemented using two facts Student fact and subject fact class. I need to fire all the rules in the decision table and I need to add all the values into array-list of the objects. But I'm getting only last rule values of decision table. It seems like decision table values are getting overridden.
Fact 1
Class StudentFact{
private int id;
private String name;
private List<SubejctFact> subjectList;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){
this.Name = name;
}
public String getName(){
return name;
}
public void setSubjectList(List<Subject> subjectList) {
this.subjectList = subjectList;
}
public int getSubjectList() {
return subjectList;
}
}
Fact 2
Class SubjectFact{
private int subId;
private String subjectName;
public void setSubId(int subId){
this.subId= subId;
}
public int getSubId(){
return subId;
}
public void setSubjectName(String subjectName){
this.subjectName = subjectName;
}
public int getSubejctName(){
return subjectName;
}
}
Current Response
{
"id":123,
"name": "xyz",
"subjectList": [
{
"id": 6,
"name":"Hindi"
},
{
"id": 6,
"name":"Hindi"
}
}
Expected Response
{
"id":123,
"name": "xyz",
"subjectList": [
{
"id": 5,
"name":"English"
},
{
"id": 6,
"name":"Hindi"
}
}
My Decision Table looks like
Any one pls advise to achieve the expected response?
Each row in a table becomes a rule, each action column becomes a row in then block.
For each rule you need a statement to create Subject, statements to populate it and statement to add it to matching student.
Values in 'CREATE' and 'COLLECT' are needed, otherwise action will be skipped.
; is required in a cell without 'target object' and it is not required when you provide '$subject' and '$student' objects. Don't ask me why. Just analyzed generated drl.
You may want to hide two 'technical columns'.
This will generate two rules like below
package draft;
//generated from Decision Table
import draft.Student;
import draft.Subject;
// rule values at A9, header at A4
rule "Rule 1"
when
$student:Student(id == "123")
then
Subject $subject = new Subject();
$subject.setSubId(5);
$subject.setSubjectName('English');
$student.addSubject($subject);
end
// rule values at A10, header at A4
rule "Rule 2"
when
$student:Student(id == "123")
then
Subject $subject = new Subject();
$subject.setSubId(6);
$subject.setSubjectName('Hindi');
$student.addSubject($subject);
end
PS: I was struggling with " being automatically replaced by Calc editor to `` which was not valid symbol for drools parser, so I used single quotes, which appeared to be special symbol at the start of a cell in the editor and skipped. So actual cell value which finally worked for me was ''English'.
Here are my models
public class Student {
private int id;
private String name;
private List<Subject> subjectList = new ArrayList<>();
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void addSubject(Subject subject) {
subjectList.add(subject);
}
public void setSubjectList(List<Subject> subjectList) {
this.subjectList = subjectList;
}
public List<Subject> getSubjectList() {
return subjectList;
}
}
public class Subject {
private int subId;
private String subjectName;
public void setSubId(int subId) {
this.subId = subId;
}
public int getSubId() {
return subId;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getSubejctName() {
return subjectName;
}
}
test
#DroolsSession(resources = "draft/ApplicableSubjects.xls",
builderProperties = "drools.dump.dir = target/dump")
public class PlaygroundTest {
#Rule
public DroolsAssert drools = new DroolsAssert();
#Test
public void testIt() {
drools.insertAndFire(new Student(123, "Student 123"));
drools.printFacts();
}
}
test output
00:00:00 --> inserted: Student[id=123,name=Student 123,subjectList=[]]
00:00:00 --> fireAllRules
00:00:00 <-- 'Rule 1' has been activated by the tuple [Student]
00:00:00 <-- 'Rule 2' has been activated by the tuple [Student]
00:00:00 Facts (1):
Student[id=123,name=Student 123,subjectList=[draft.Subject#1ded7b14, draft.Subject#29be7749]]
I have to update my resources partially using PATCHrequest whose body is a JSON. Below is my POJO for OwnerDetails. I am using play-framework with Hibernate.
public class OwnerDetailsVO {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
I have created rows in MySQL for the Entity Object which corresponds to this value object (VO).
My JSON body for PATCH request is,
PATCH /owners/123
[
{ "op": "replace", "path": "/name", "value": "new name" }
]
I have configured the correct route to the method in the routes file.
Here is the OwnerController class which should process the JSON request. I am using POSTMAN to send the requests.
public class OwnerController extends Controller {
public Result create() {
Form<OwnerDetailsVO> odVOForm = Form.form(OwnerDetailsVO.class).bindFromRequest();
if(odVOForm.hasErrors()) {
return jsonResult(badRequest(odVOForm.errorsAsJson()));
}
OwnerDetailsVO odVO = odVOForm.get();
int id = odProcessor.addOwnerDetails(odVO);
return jsonResult(ok(Json.toJson("Successfully created owner account with ID: " + id)));
}
public Result update(int id) {
//I am not sure how to capture the data here.
//I use Form to create a new VO object in the create() method
}
}
How should the request be captured inside the update() function so that I can partially update my resource? I am not able to find good documentations to know about PATCH operations for Play! Framework.
Edit: I have seen about WSRequest for Patch operation, but I am not sure how to use that. Will that be helpful?
This is a sample code using ebeans in Play Framework
public Item patch(Long id, JsonNode json) {
//find the store item
Item item = Item.find.byId(id);
if(item == null) {
return null;
}
//convert json to update item
Item updateItem;
updateItem = Json.fromJson(json, Item.class);
if(updateItem.name != null){
item.name = updateItem.name;
}
if(updateItem.price != null){
item.price = updateItem.price;
}
item.save();
return item;
}
this is my current code to store rooms(it compiles fine) but in the UML there is a variable called addEquipment and there is also another class called Equipment to be defined. I'm having trouble wrapping my head around what I'm supposed to do with this. Am I supposed to create and call an object called Equipment? what goes in addEquipment?
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private String equipmentList;
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public String getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(String anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
}
//Create room object
public Room(int capacity, String equipmentList) {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
}
}
You can create a new class Equipment and modify your attribute equipmentList to be a List:
public class Equipment {
private String name;
public Equipment(String name) {
this.name = name;
}
}
public class Room {
//begin variable listing
private String name;
private int id;
private int capacity;
private List<Equipment> equipmentList = new ArrayList<Equipment>();
//begins get methods for variables
public String getName(){
return name;
}
public int getID(){
return id;
}
public int getCapacity(){
return capacity;
}
public List<Equipment> getEquipmentList(){
return equipmentList;
}
// Set the variables
public void setName(String aName){
name=aName;
}
public void setID(int anID){
id=anID;
}
public void setCapacity(int aCapacity){
capacity=aCapacity;
}
public void setEquipmentList(List<Equipment> anEquipmentList){
equipmentList=anEquipmentList;
}
public String addEquipment(String newEquipment, String currentEquipment){
Equipment oneEquipment = new Equipment(newEquipment);
equipmentList.add(oneEquipment);
}
//Create room object
public Room() {
setCapacity(capacity);
setEquipmentList(equipmentList);
}
//Convert variables to string version of room
public String toString(){
String capacity=String.valueOf(getCapacity());
String room = "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
return room;
}
}
In the method addEquipment, you can create a new Equipment and add it to equipmentList, like code above.
An Equipment class could be anything. Lets assume the "Equipment"-class has a String called "name" as it's attribute
public class Equipment {
String name;
public Equipment( String name) {
this.name = name;
}
public String getName() {
return this.name
}
}
When you extend your Room class by the requested "addEquipment" method, you can do something like this.
public class Room {
... // Your code
private int equipmentIndex = 0;
private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects
public void addEquipment( Equipment eq ) {
if ( equipmentIndex < 10 ) {
equipment[ equipmentIndex ] = eq;
equipmentIndex++;
System.out.println("Added new equipment: " + eq.getName());
} else {
System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
}
}
}
Now when you call
room.addEquipment( new Equipment("Chair") );
on your previously initialized object of the Room-class, you will get
"Added new equipment: Chair"
Hope this helps a bit.
PS: The code is untestet (maybe there hides a syntax error somewhere)
I am working on Spring. I am unable to display the list items in JSP. It says: Property not found on type java.lang.String.
I have a POJO class Student:
public class Student {
private Integer age;
private String name;
private Integer id;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
In my controller class I fetch list of students and assign it to a List and add the list to the model attribute. Which is as follows.
#RequestMapping("/getstuds")
public String getstudents(#ModelAttribute Student student, ModelMap model){
StudentDAO studentDAO = (StudentJDBCTemplate)applicationContext.getBean("studentJDBCTemplate");
List<Student> students = studentDAO.listStudents();
model.addAttribute("studlist",students);
System.out.println(students.iterator().next().getName());
return "showstuds";
}
showstuds.jsp
<table>
<c:forEach var="stud" items="${studlist} }">
<tr><td>${stud.Name}</td></tr>
</c:forEach>
</table>
I get the following exception:
javax.el.PropertyNotFoundException: Property 'Name' not found on type com.spring.mvc.Student
your variable name is name not Name
<tr><td>${stud.name}</td></tr>
instead of
<tr><td>${stud.Name}</td></tr>
And also remove the brace
items="${studlist}"
instead of
items="${studlist} }"
Google endpoint message objects are very simple POJOs. I have a compound POJO that used to work but now is no longer working. The error I am getting, when android client makes the call, is that the JSON cannot be parsed because of AnimalTag. Here are the POJOs. To migrate to Java 7, I copied and pasted the code manually. So I thought that could have been the cause, that perhaps I left something out. But I can't think of what the problem may be. Other calls work fine. But this one keeps failing.
The usage is that the method receives Dog from client to save on server. Not all the data in Dog is filled, but many, including AnimalTag, are filled. Also, AnimalTag only has the manufacturer filled. Again This all used to work.
public class Dog {
private String name;
private String owner;
private AnimalTag tag;
public Dog(String name, String owner, AnimalTag tag) {
super();
this.name = name;
this.owner = owner;
this.tag = tag;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return this.owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public AnimalTag getTag() {
return this.tag;
}
public void setTag(AnimalTag tag) {
this.tag = tag;
}
}
class AnimalTag{
private long number;
BlobKey imageKey;
String manufacturer;
public AnimalTag(long number, BlobKey imageKey, String manufacturer) {
super();
this.number = number;
this.imageKey = imageKey;
this.manufacturer = manufacturer;
}
public long getNumber() {
return this.number;
}
public void setNumber(long number) {
this.number = number;
}
public BlobKey getImageKey() {
return this.imageKey;
}
public void setImageKey(BlobKey imageKey) {
this.imageKey = imageKey;
}
public String getManufacturer() {
return this.manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
I got the answer, AnimalTag was missing the following constructor:
public AnimalTag(){}