Hi all,
I’ve been thinking much about this MVC thing. From my point of view, we should not focus on JSF only, instead take a look at some more specs like JAX-RS, Servlet/JSP and JSF.
I think many of the samples given by Adrian can be implemented in Java EE by simply forwarding from JAX-RS to the Faces Servlet.
Use a JAX-RS resource. Define the model with @Model (which is actually @Named @RequestScoped), inject it in your JAX-RS bean and simply use it from JSF.
Then you can do something like this (the code isn’t tested):
@Path("/exemple/clients")
public class ClientResource {
@Inject
private ClientModel model; // ClientModel is annotated with @Model
@Inject
private ClientManager clientManager;
@GET
@Path("{id}")
public void show(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("id") String id) throws Exception {
Client lClient = clientManager.findById(id);
model.setClient(lClient);
model.setItemId(id);
request.getRequestDispatcher("/exemple/clients/show.faces").forward(request, response);
}
}
We should simply collect SpringMVC examples and port them to Java EE. I’m willing to do so, but I don’t have experience with SpringMVC. So examples should be provided by others.
Maybe we can add them to
https://github.com/javaee-samples/javaee7-samples or create a dedicated github-repo for it.
Ciao Frank
Am 01.03.2014 um 21:57 schrieb Adrian Gonzalez <adr_gonzalez_at_yahoo.fr>:
> [1] An action-based controller with Spring MVC
> @Controller
> @RequestMapping(value = "/exemple/clients")
> public class ClientController {
>
> @Inject
> private ClientManager clientManager;
>
>
> @RequestMapping(value = "/{id}", method = RequestMethod.GET)
> public String show(@PathVariable Long id, Model uiModel) {
> Client lClient = clientManager.findById(id);
> uiModel.addAttribute(lClient);
> uiModel.addAttribute("itemId", id);
> return "exemple/client/show";
> }
>
> @RequestMapping(method = RequestMethod.POST)
> public String create(@Valid Client client, BindingResult bindingResult,
> Model uiModel) {
> String inputView = "exemple/client/edit";
> if (bindingResult.hasErrors()) {
> return inputView;
> }
> client.setDateContact(new Date());
> try {
> clientManager.create(client);
> } catch (SomeBusinessException e) {
> bindingResult.reject("error.global", e.getMessage());
> return inputView;
> }
> uiModel.asMap().clear();
> return "redirect:/exemple/clients/" + client.getId();
> }
>
> @RequestMapping(params = "form", method = RequestMethod.GET)
> public String createForm(Model uiModel, SitePreference sitePreference) {
> uiModel.addAttribute(newClient());
> return "exemple/client/edit";
> }
>
> @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
> public String delete(@PathVariable Long id, Model uiModel, SitePreference sitePreference) {
> Client client = clientManager.findById(id);
> clientManager.delete(client);
> uiModel.asMap().clear();
> return "redirect:/exemple/clients" + (sitePreference.isMobile() ? "-mobile" : "");
> }
>
> @RequestMapping(method = RequestMethod.PUT)
> public String update( @Valid Client client, BindingResult bindingResult,Model uiModel) {
> String inputView = "exemple/client/edit";
> if (bindingResult.hasErrors()) {
> return inputView;
> }
> try {
> clientManager.update(client);
> } catch (MatriculeExistantException e) {
> bindingResult.reject("error.global", e.getMessage());
> return inputView;
> }
> uiModel.asMap().clear();
> return "redirect:/exemple/clients/" + client.getId();
> }
>
> @RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
> public String updateForm(@PathVariable Long id, Model uiModel) {
> show(id, uiModel);
> return "exemple/client/edit";
> }
>
> @RequestMapping(method = RequestMethod.GET)
> public String list(@Valid ClientCriteriaForm clientCriteriaForm,
> BindingResult aBindingResult,
> Model uiModel) {
> if (aBindingResult.hasErrors()) {
> return "exemple/client/list";
> }
> ...call clientManager...
> uiModel.addAttribute("clients", clients);
> return "exemple/client/list";
> }
>
> @RequestMapping(params = "list", method = RequestMethod.GET)
> public String listForm(ClientCriteriaForm clientCriteriaForm, Model uiModel) {
> List<Client> clients = new ArrayList<Client>();
> uiModel.addAttribute("clients", clients);
> return "exemple/client/list";
> }
>
> @RequestMapping(params="cancel")
> public String cancel() {
> return "redirect:/exemple/clients";
> }
> }
>
> [2] A REST backend controller with Spring MVC
> @Controller
> @RequestMapping(value = "/exemple/rest/clients")
> public class RestClientController {
>
> @Inject
> private ClientManager clientManager;
>
>
> @RequestMapping(method = RequestMethod.GET)
> @ResponseBody
> public Map<String, Object> list(@Valid ClientCriteriaForm clientCriteriaForm) {
> List<Client> clients = clientManager.findByName(
> clientCriteriaForm.getNom(), clientCriteriaForm.getPage()
> * clientCriteriaForm.getSize(),
> clientCriteriaForm.getSize() + 1);
> Map<String, Object> lResults = new HashMap<String, Object>();
> if (clients.size() > clientCriteriaForm.getSize()) {
> lResults.put("hasNextPage", true);
>
> lResults.put("page", clientCriteriaForm.getPage());
> clients.Remove(clientCriteriaForm.getSize().intValue());
> }
> lResults.put("clients", clients);
> return lResults;
> }
>
> @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces="application/json")
> @ResponseBody
> public Client show(@PathVariable Long id) {
> return clientManager.findById(id);
> }
>
> @RequestMapping( method = RequestMethod.POST, produces="application/json")
> @ResponseBody
> public Client create(@RequestBody @Valid Client client) throws SomeBusinessException {
> return clientManager.create(client);
> }
>
> @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces="application/json")
> @ResponseStatus(HttpStatus.NO_CONTENT)
> public void delete(@PathVariable Long id) {
> Client client = clientManager.findById(id);
> clientManager.delete(client);
> }
>
> @RequestMapping(method = RequestMethod.PUT, produces="application/json")
> @ResponseBody
> public Client update(@RequestBody @Valid Client client) throws SomeBusinessException {
> return clientManager.update(client);
> }
>
>
> @ExceptionHandler(RequestBodyNotValidException.class)
> @ResponseBody
> public Map<String,Object> handleException(RequestBodyNotValidException exception, HttpServletResponse response) throws IOException {
> return RestUtils.convertAndSetStatus(exception, response);
> }
>
> @ExceptionHandler(Exception.class)
> @ResponseBody
> public Map<String,Object> handleException(Exception exception, HttpServletResponse response) throws IOException {
> return RestUtils.convertAndSetStatus(aException, aResponse);
> }
> }
>