0

I was following example tutorials how to integrate JSF, Spring, Hibernate in my web application:

http://www.journaldev.com/7122/jsf-spring-hibernate-integration-example-tutorial

http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial

Everything til now works fine for me (insert and delete). Now, I would like to have that single form not only for inserting data into database, but use is also for editing, only without need for entering id field manually in html text field as it is shown in examples. Also, I would like to do this without seo path like edit/id (I want it like edit?id=1)

I see in the second link I posted here in my question, that there is controller class defined for that for that - PersonController.java (that example uses Spring MVC, I am using JSF like the first link tutorial is showing, but I think I need that controller)

In there it is defined:

@RequestMapping("/edit/{id}")
    public String editPerson(@PathVariable("id") int id, Model model){
        model.addAttribute("person", this.personService.getPersonById(id));
        model.addAttribute("listPersons", this.personService.listPersons());
        return "person";
    }

But I do not nowhere see in the .java files or in .jsf/.xhtml files where that method editPerson was called? I know I should wrote it probably whit request mapping like this:

@RequestMapping(value = "/index.xhtml", method = RequestMethod.GET, params = { "id" })

I wrote that same method with that RequestMapping, but what to do now? Where must I call that method to populate my insert form with data when GET param called "id" is present in url? I want to make functionallity when GET param called id is present, then filled form with data (for that id in the database) will show; and after saving that filled form, update will occur in database, not insert. Insert should be occured when no id param is in the url (GET method). I hope I was clear enough.

Can someone show me on source code how to do it?

DarioBB
  • 663
  • 2
  • 8
  • 29
  • We are Borg I do not use JSP, i use JSF (.xhtml and .jsf) files. I do not think you realised what I need. I know how to send my param to url. I do not know how, when the id is present in the url, show corresponding populated data from database (instead of blank form) ? – DarioBB Sep 24 '15 at 12:12
  • No idea what you're concretely asking as nothing makes sense here, but this is definitely your food for read: http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc Nonetheless, the real JSF answer (without confusing Spring MVC noise) would be http://stackoverflow.com/questions/8459903/creating-master-detail-pages-for-entities-how-to-link-them-and-which-bean-scope Which of those do you accept as duplicate? – BalusC Sep 24 '15 at 12:48

1 Answers1

0

Try this code:

    @RequestMapping(value = "/index.xhtml", method = RequestMethod.GET)
    public String editPerson(@RequestParam int id, Model model){
        model.addAttribute("person", this.personService.getPersonById(id));
        model.addAttribute("listPersons", this.personService.listPersons());
        return "person";
    }
Alex Barkun
  • 503
  • 6
  • 13
  • I don't have to call editPerson() anywhere in the code? You say that it will work just defining it like this and not calling it; and it will update my form when I try to save it? – DarioBB Sep 24 '15 at 12:30
  • Is "person" partial view or complete web page? I mean will you substitute part of view with this response or the whole page? If the whole, then you can just open "/index.html?id=1" in web browser or put it in a html link. – Alex Barkun Sep 24 '15 at 12:35
  • I do not need substitute part of view with this response. I need this what you said: "then you can just open /index.html?id=1" Just to be sure.. how spring/jsf/hibernate knows method must be called editPerson to populate RequestMapping parameters with it? I don't understand.. i could be anything wrote instead of editPerson if this is correct? – DarioBB Sep 24 '15 at 12:47
  • Params that go after **?** symbol are called request parameters . In our case **?id=1** we have **id** request parameter and Spring will parse it and populate your method's **id** parameter with value **1**. Better to rename url "/index.xhtml" to "/editPerson.xhtml" and Spring will exactly go to this specific method. – Alex Barkun Sep 24 '15 at 12:52
  • See [Spring docs](http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam) to understand how it works. – Alex Barkun Sep 24 '15 at 12:59
  • Thank you, I will try it. You say I must rename index.xhtml to editPerson.xhtml..why rename file? I would like to have single form file for everything. Or did you think just put it like this @RequestMapping(value = "/editPerson.xhtml".. ? – DarioBB Sep 24 '15 at 13:00
  • It depends on site architecture. If you have one page or it's test application , then "index.xhtml" is fine. If you have lots of pages, then "index.xhtml" should lead to some dashboard and each page should have unique name or at least @RequestMapping(value = "/person") should be added to controller class in order to avoid name clashes and overwriting. Urls should be unique. Sorry, if my explaining is a bit messy) – Alex Barkun Sep 24 '15 at 13:06
  • I was talking about url in @RequestMapping. If you have "editPerson.xhtml" file, then you should return the view name in return statement: `return "editPerson";`. – Alex Barkun Sep 24 '15 at 13:15
  • It still doesen't work. I do not know where is the problem. In my .jsf page, attributes are named like this, based on "items" bean: But nothing gets filled when defining that method in controller class. I tried in various options - none of it from spring docs works..what am I missing? Should I do something else, somewhere? – DarioBB Sep 24 '15 at 15:30
  • You need to create a separate question with code of your beans, configuration and controller. JSF has it's own mechanism to work with views and it's a bad practice to use Spring MVC as a view, but again if it's your strict requirement then create a new question with additional info. It's hard to say where the problem is without that info. Thank you. – Alex Barkun Sep 24 '15 at 15:36
  • Thank you..and i would not like to mix JSF with Spring MVC - no, no no. I tought it is only way to do it..Can I avoid that? I would like to use instead something that basic Spring/Hibrnate/Jsf offers to populate form, what can i do instead? – DarioBB Sep 24 '15 at 16:12
  • I would suggest to avoid JSF and use pure Spring Boot, MVC and JPA/Hibernate. Start with this tutorial [link](https://spring.io/guides/gs/handling-form-submission/), than add DB layer with Hibernate. Spring has lots of great articles and code of examples on github. And probably at first you can avoid using theirs Thymeleaf(view technology, adds enhancements to html to iterate over collections passed from controller and so on) and use pure JSP/jstl. – Alex Barkun Sep 24 '15 at 16:15
  • Also review [this](https://github.com/spring-projects/spring-petclinic), it's pet clinic, Spring team's showcase project. – Alex Barkun Sep 24 '15 at 16:24
  • Shurik, thank you for advice, but I must do this with JSF :( – DarioBB Sep 24 '15 at 17:23
  • And how's about to get rid of Spring MVC and use JSF's @ManagedBean? In most cases people use Spring's DI on service/DAO layers. And JSF is used on controller/view level. – Alex Barkun Sep 24 '15 at 17:24
  • I do not know how to do that.. where can I check that? – DarioBB Sep 24 '15 at 17:30
  • You can start from [here](http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/) and read other relative tutorials. – Alex Barkun Sep 24 '15 at 17:37