I'm new to Spring, I fear this question may be duplicate, as there are numerous ways to do get resource from database to drop-down which I don't understand, so I'm asking if anyone can help me by fixing my code or with their own code which matches with mine as it'll be easy for me to learn.
My POJO
#Entity
#Table(name = "emp69")
public class Emp {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String designation;
....Constructor with name & designation field...
......getters method and setters method.....
My Repository
public interface EmpRepository extends JpaRepository<Employee, Integer> {
#Modifying
#Query(
value = "select designation from employee",
nativeQuery = true
)
List<String> designation();
}
My Data Access Object
#Service
public class EmployeeDao {
#Autowired
private EmpRepository repo;
#Transactional
public List<String> desig() {
return repo.designation();
}
}
My Controller
#Controller
public class EmployeeController {
#Autowired
EmployeeDao dao;
#RequestMapping("/empform")
public String showform(Model m) {
List<String> designation= dao.designation();
m.addAttribute("designation", designation);
return "empform";
}
}
My JSP page
<select>
<c:forEach var="dd" items="${designation}">
<option><c:set var="dd.designation">${dd.designation }</c:set></option>
</c:forEach>
</select>
seems everything fine to me but when I execute, it gives
Property [designation] not found on type [java.lang.String]
error.
And on STS console
javax.el.PropertyNotFoundException: Property [designation] not found on type [java.lang.String]
Your Controller, Service & Repository part are correct but it seems like you haven't studied JSP well. I'll suggest you to take a step back from a big framework and individually learn each of its aspects first like JSP, JSTL, etc. Following this path will lead you to understanding of big frameworks and everything will be more clear.
Anyhow here's your answer..
<select>
<c:forEach var="dd" items="${designation}">
<option><c:set var="d" value="${dd}" /><c:out value="${d }"/></option>
</c:forEach>
</select>
just replace this part in your code..
Hope it helps
Related
We have the following situation: an entity and a pojo in Objectify v5.
As you can see, the entityclass has a reference to the pojo, like this:
#Entity
public class TestCustomer {
#Id
public Long id;
TestIbj ibj;
}
class TestIbj {
TestCustomer customer;
}
This fails with a StackOverflowError when we try to save it like this:
TestCustomer testCustomer = new TestCustomer();
OfyService.ofy().save().entity(testCustomer).now();
Error:
java.lang.StackOverflowError
at java.lang.Class.getMethod0(Class.java:2772)
at java.lang.Class.isCheckMemberAccessOverridden(Class.java:2214)
at java.lang.Class.checkMemberAccess(Class.java:2233)
at java.lang.Class.getDeclaredMethods(Class.java:1854)
at...
Our question is: Why is this not allowed/failing? If we change the pojo into an Entity, it works fine, but we don't understand why that is..
Thank you very much!
StackoverflowError almost always means that there is an unbounded recursion. It's the case in your code as well:
#Entity
public class TestCustomer {
#Id
public Long id;
TestIbj ibj; // TestCustomer contains TestIbj
}
class TestIbj {
TestCustomer customer; // TestIbj contains TestCustomer (which further contains TestIbj.. and so on)
}
Due to this circular dependency (shown in code above), objectify will never be able to construct object graph.
I am trying to send parameters from UI to Spring MVC controller. My parameter looks like
caseId=23951910&serviceProvided%5B0%5D.id=25989&serviceProvided%5B0%5D.desc=24-Hour+Service&serviceProvided%5B0%5D.duration=1&serviceProvided%5B0%5D.pages=--&serviceProvided%5B1%5D.id=25988&serviceProvided%5B1%5D.desc=3rd+Party+Contact&serviceProvided%5B1%5D.duration=2&serviceProvided%5B1%5D.pages=--&serviceProvided%5B2%5D.id=25980&serviceProvided%5B2%5D.desc=Advice&serviceProvided%5B2%5D.duration=3&serviceProvided%5B2%5D.pages=--&serviceProvided%5B3%5D.id=25982&serviceProvided%5B3%5D.desc=Document+Preparation&serviceProvided%5B3%5D.duration=4&serviceProvided%5B3%5D.pages=--&serviceProvided%5B4%5D.id=DOCREVIEW&serviceProvided%5B4%5D.desc=Document+Review&serviceProvided%5B4%5D.duration=5&serviceProvided%5B4%5D.pages=6
To match this parameter I am using custom class as
Class MyDto {
private Long caseId;
private List<ServiceProvided> serviceProvided;
//getter and setter
}
Class ServiceProvided {
private String id;
private String desc;
private Long duration;
private Long pages;
//getter and setter
}
I have controller as
#RequestMapping(value = "/cases/resolveClaim.htm", method = RequestMethod.POST)
public ModelAndView createClaim(#ModelAttribute("claimInfo") MyDto myDto, BindingResult result) { ... }
I am getting 404 error so I am guessing "serviceProvided" list couldn't match to myDto. So my questions are:
Is this a really a reason I am getting 404 error?
If yes I guess I have to solve with PropertyEditor or Converter? Am I correct?
Is there any example code that I can refer to?
Thanks
I need to set some private fields in an object using another object's fields. Those two objects may not be instances of same class.
What I see from a short reading, I can use Apache's BeanUtils and Spring's ReflectionUtils for that. I couldn't find a satisfying explanation for them regarding security, performance, support etc.
The solution will be used in production environment too, so I need a concrete solution.
Which approach do you suggest for such a task.
I think you need use just the BeanUtils library. See my sample, i do a copy properties from CustomerBean to SellerBean.
package testes.beanutils;
import org.apache.commons.beanutils.BeanUtils;
public class Main {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.setId((long)1);
customer.setName("Bruno");
customer.setLastname("Tafarelo");
Seller seller = new Seller();
BeanUtils.copyProperties(seller, customer);
System.out.println(customer);
System.out.println(seller);
}
}
class Customer {
private Long id;
private String name;
private String lastname;
//getters and setters
//toString
}
class Seller {
private Long id;
private String name;
private int sales;
//getters and setters
//toString
}
All,
I have an instance of ProjectBudget class returned from a web method.
Ex:
[WebMethod()]
public ProjectBudget LoadBudget(int id)
{
ProjectBudget budget = BudgetManager.LoadBudget(id);
return budget;
}
The ProjectBudget class contains the following defintion:
public class ProjectBudget
{
public int Id = -1;
public long VersionNumber = -1;
public string QuoteNumber = "";
public string CurrencyCode = "";
public ProjectInfo Project;
public ClientInfo Client;
public readonly List<InventoryItem> Inventory = new List<InventoryItem>();
public readonly List<Staff> Staff = new List<Staff>();
public readonly List<CodeType> Departments = new List<CodeType>();
public readonly SerializableDictionary<string, string> Tasks = new SerializableDictionary<string, string>();
public ProjectBudget()
{
}
}
All public fields you see are serialized just fine with the exception of Tasks field, which is completely ignored by XML serializer. Since we all know by now that Dictionaries cannot be handled by XML serializer, I use a serializable dictionary (which is just a dictionary that implements IXmlSerializable) here but XML serializer decides to ignore it completely, i.e. the XML output does not contain any tasks and the generated proxy class doesn't have this field.
I need to figure out how to tell the XML serializer not to omit this field.
Btw, what is interesting is that a web method that returns SerializableDictionary works fine!
A very similar question as yours appears to have been asked already: Link.
Use DataContractSerializer or try explicitly implementing your getter (and setter), as per this link.
Thanks in advance for any help.
I have a form that is being validated with JSR 303. After validation fails, the controller returns the form, shows validation errors, and renders the form with the original values. This works fine with all types of form elements except the mutli-select element.
The command object:
public class TaskOrder implements Serializable {
private static final long serialVersionUID = 1L;
...
#XmlTransient
#ManyToMany
#<OtherJPAAnnotations...>
private List<Contractor> subcontractors;
...
}
Contractor class:
public class Contractor implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#<OtherJPAAnnotations...>
private Integer id;
#<OtherJPAAnnotations...>
private String name;
}
Controller:
#RequestMapping(value="processingPath", method=RequestMethod.POST)
public String createNewTaskOrder(#Valid #ModelAttribute TaskOrder taskOrder,
BindingResult result,
Model model) {
...
if (!result.hasErrors()) {
//No binding errors, lots of processing...
else {
model.addAllAttributes(result.getModel());
model.addAttribute(taskOrder);
model.addAttribute("subs", myDAOInstance.getSubs());
return this.setupNewTaskOrder(model);
}
}
#RequestMapping("getFormPath")
public String setupNewTaskOrder(Model model) {
if (!model.containsAttribute("taskOrder")) {
TaskOrder taskOrder = new TaskOrder();
taskOrder.setId(0);
model.addAttribute(taskOrder);
}
return "_n/admin/taskOrder/new";
}
The form:
<form:form commandName="taskOrder" action="processPath">
...
<form:select path="subcontractors">
<form:options items="${subs}" itemValue="id" itemLabel="name"/>
</form:select>
...
</form>
When I open an existing "TaskOrder" with the same form, the values are selected in the "subcontractors" multi-select.
However, trying to add a new "TaskOrder", when it returns from validation the values aren't selected. I know that the selected values are being attached to the model and returned, but just not being selected in the select element.
Thanks for any help!
Thanks for the help. Biju...you were correct!
For those who may stumble across this issue, I added the "EqualsUtil" class described here to my project:
http://www.javapractices.com/topic/TopicAction.do?Id=17
Then added the following method to my Contractor class:
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Contractor)) return false;
Contractor c = (Contractor) o;
//Here I ignore the other properties since "id" and "name" are what
//I'm primarily concerned with...
return EqualsUtil.areEqual(this.name, c.name) &&
EqualsUtil.areEqual(this.id, c.id);
}