Announcement Announcement Module
No announcement yet.
Invoking a generic java method on an entity Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Invoking a generic java method on an entity

    Hi, I'm totally new to Spring, and, unfortunately, pretty new to web applications development as well.

    I created a roo application from scratch. I created an entity named Customer. I ran “web mvc all” to obtain my shiny web app. Everything fine until now (persistence methods to add/retrieve entities working fine).
    Now let’s go to the question. I have a web service that I want to invoke from within my webapp. The web service receives an input of type Customer. What I would like to implement is a new menu entry under “Create new Customer” and “List all Customers” in my web app. That item should be able to direct me to a page where I can choose from a drop-down list a customer from the ones in the database. Clicking on a button, i'd like to invoke the web service passing the selected entity (or a java class whose members can be initialized from it) to the web service.

    I managed to edit menu.jspx to add a menu entry.

    I edited the CustomerController adding:
        @RequestMapping(params = "rule", produces = "text/html")
        public String CustomerController.rule(Model uiModel) {
        	List<String[]> dependencies = new ArrayList<String[]>();
            if (Customer.countCustomers() == 0) {
                dependencies.add(new String[] { "customer", "customers" });
            uiModel.addAttribute("dependencies", dependencies);
            return "customers/rule";
    I created a Java class supermarket.web.SupermarketDecisionService that will implement the methods required to invoke the web service. It has an attribute of type Customer.

    I created a page rule.jspx looking like this:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="" version="2.0">
        < contentType="text/html;charset=UTF-8"/>
        <jsp:output omit-xml-declaration="yes"/>
        <form:rule id="fr_supermarket_domain_customer_Customer" modelAttribute="customer" path="/customers" z="?">
            <field:select field="supermarketdecisionservice" id="c_supermarket_web_SupermarketDecisionService_customer" itemValue="id" items="${customers}" path="/customers" required="true" z="?"/>
    I copy/pasted the form/create.tagx to a new form/rule.tagx.

    As is, my edited application is able to load the page I created and display the drop-down list of customers. This is working only if I set disableFormBinding=”true” in the field:select tag, but the ideal behavior should be that the selected item binds to the customer attribute of SupermarketDecisionService.

    I’m stuck with a “Neither BindingResult nor plain target object for bean name 'customer' available as request attribute” error whenever I visit that page without setting disableFormBinding=”true”.

    I have also no idea about how to tell my web app that when i click that button, that particular method should be invoked.

    Do I made any horrible design error? How should i proceed?


  • #2
    Create an instance directly by supplying the generic type information with the entity. For example the following code shows how to create a response containing the result of a method invoked via reflection:
    Method method = ...;
    GenericEntity<Object> entity = new GenericEntity<Object>(
    method.invoke(...), method.getGenericReturnType());
    Response response = Response.ok(entity).build();


    • #3
      Thank you for your reply Sentikakar. I'll give it a try.
      For now, i managed to obtain what i needed by adding to the controller another method that invokes the web service and resdirects to a page where web service execution results are displayed. Maybe this approach is more "MVC compliant" too, as the controller executes the invokation, and the web page only displays results.