Getting started with Spring MVC

Categories: Java; Tagged with: ; @ August 11th, 2013 12:00

1. Config pom.xml

2. Add mvc-dispatcher in web.xml

3. Config dispatcher xml, i.e.

        <context:component-scan base-package="com.liguoliang.jee.web" />
 
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

4. Controler:

package com.liguoliang.jee.web;


@Controller
@RequestMapping("/OrgMgt")
public class OrgMgtController {
	@RequestMapping(method=RequestMethod.GET, value={"", "view"})
	public String listOrgs(ModelMap modelMap) {
		return "OrgsList"; // JSP
	}
	
	@RequestMapping(value="new", method=RequestMethod.GET)
	public String createOrg(ModelMap modle) {
		Org initOrg = … // init new Org object.
		modle.addAttribute("newOrg", initOrg);
		return "NewOrg";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String onSubmit(@ModelAttribute("org") Org org, BindingResult result) {
		OrgValidator validator = new OrgValidator();
		validator.validate(org, result);
		if(result.hasErrors()) {
			return "NewOrg";
		}
		
		return "redirect: OrgMgt";
	}
	

	@RequestMapping(value="view/{id}", method=RequestMethod.GET)
	public String viewOrg(ModelMap modelMap, @PathVariable Integer id) {
		modelMap.addAttribute("id", id);
		return "OrgDetails";
	}
}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.