Der Controller

Ein Spring-Web-Controlller besteht meistens aus drei Elementen:

Dependency Injections

@Controller
public class SeedStarterMngController {
@Autowired
private VarietyService varietyService;
@Autowired
private SeedStarterService seedStarterService;
  ...
}

Model Attributes

@ModelAttribute("allTypes")
public List<Type> populateTypes() {
  return Arrays.asList(Type.ALL);
}
@ModelAttribute("allFeatures")
public List<Feature> populateFeatures() {
  return Arrays.asList(Feature.ALL);
}
@ModelAttribute("allVarieties")
public List<Variety> populateVarieties() {
  return this.varietyService.findAll();
}
@ModelAttribute("allSeedStarters")
public List<SeedStarter> populateSeedStarters() {
  return this.seedStarterService.findAll();
}

Mapped Methods

@RequestMapping({"/","/seedstartermng"})
public String showSeedstarters(final SeedStarter seedStarter) {
  seedStarter.setDatePlanted(Calendar.getInstance().getTime());
  return "seedstartermng";
}
@RequestMapping(value="/seedstartermng", params={"save"})
public String saveSeedstarter(
   final SeedStarter seedStarter, final BindingResult bindingResult, final ModelMap model) {
  if (bindingResult.hasErrors()) {
    return "seedstartermng";
  }
  this.seedStarterService.add(seedStarter);
  model.clear();
  return "redirect:/seedstartermng";
}

Controller-Weiterleitung

In Spring kann im Kontroller gleich der name eines anderen Kontroller zurückgegeben werden um zur Entsprechenden View zu navigieren.

Beiepiel (Java-code):

public class ExampleController {
  @RequestMapping("/data")
   public String getData(Model model) { ... return "template" }
  @RequestMapping("/data")
  public String getDataParam(@RequestParam String type) { ... return "template" }
}

Beispiel (HTML-Code):

<a th:href="${(#mvc.url('EC#getData')).build()}">Get Data Param</a>
<a th:href="${(#mvc.url('EC#getDataParam').arg(0,'internal')).build()}">Get Data Param</a>

You can read more about this mechanism at http://docs.spring.io/spring-framework/docs/4.1.2.RELEASE/springframework-reference/html/mvc.html#mvc-links-to-controllers-from-views

results matching ""

    No results matching ""