Springmvc构造RESTful

web.xml
    <servlet>
        <servlet-name>annomvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:resource/spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>annomvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


类头:
@Controller
@RequestMapping("/maintain/bms/")
public class BmsPressController {


写法1:
    @RequestMapping(value="press_insert_load/{id}",method=RequestMethod.GET)
    public ModelAndView load(@PathVariable("id") int id,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BmsPress bmsPress = bmsPressService.selectById(id);
        request.setAttribute("bmsPress", bmsPress);

        modelAndView.setViewName("/maintain/bms/press_insert_load");
        return modelAndView;
    }


写法2:
    @RequestMapping(value="press_insert_load/{id}",method=RequestMethod.GET)
    public String load(@PathVariable("id") int id,HttpServletRequest request){
        BmsPress bmsPress = bmsPressService.selectById(id);
        request.setAttribute("bmsPress", bmsPress);
        return "/maintain/bms/press_insert_load";
    }


上面两种写法访问方式都是 :/maintain/bms/press_insert_load/21

下面这种访问方式:/maintain/bms/press_insert_load.do?id=21
    @RequestMapping(value="press_insert_load",method=RequestMethod.GET)
    public String load(@RequestParam int id,HttpServletRequest request){
        BmsPress bmsPress = bmsPressService.selectById(id);
        request.setAttribute("bmsPress", bmsPress);
        return "/maintain/bms/press_insert_load";
    }


效果:

你可能感兴趣的:(springMVC)