springMVC-04-Controller配置总结和restful风格

Controller配置总结和restful风格

一、用controller接口实现SpringMVC

1、新建一个项目,导入web支持

2、导入相关jar包

3、编写web.xml,注册DispatcherServlet

4、编写SpringMVC配置文件

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/jsp/"/>
        
        <property name="suffix" value=".jsp"/>
    bean>

5、编写controller类

//只要实现了Controller接口的类,说明这就是一个控制器
public class ControllerTest1 implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();

        mv.addObject("msg","ControllerTest1");
        mv.setViewName("test");
        return mv;
    }
}

6、在springMVC配置文件中注册bean

<bean name="/t1" class="com.lc.controller.ControllerTest1"/>

7、测试

springMVC-04-Controller配置总结和restful风格_第1张图片

二、用注解实现SpringMVC

1、新建一个项目,导入web支持

2、导入相关jar包

3、编写web.xml,注册DispatcherServlet

4、编写SpringMVC配置文件


<context:component-scan base-package="com.lc.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
    
    <property name="prefix" value="/WEB-INF/jsp/"/>
    
    <property name="suffix" value=".jsp"/>
bean>

5、编写controller类

@Controller
public class ControllerTest2 {
    @RequestMapping("/t2")
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest2");
        return "test";
    }
    @RequestMapping("/t3")
    public String test2(Model model){
        model.addAttribute("msg","ControllerTest3");
        return "test";
    }
}
@Controller
@RequestMapping("/t3")
public class ControllerTest3 {
    @RequestMapping("/t2")
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest3+2");
        return "test";
    }
}

6、测试

springMVC-04-Controller配置总结和restful风格_第2张图片
springMVC-04-Controller配置总结和restful风格_第3张图片
springMVC-04-Controller配置总结和restful风格_第4张图片

三、restful风格

1、编写controller类

@Controller
public class RestFulController {

    //原来的:http://localhost:8080/add?a=1&b=2
    //RestFul:http://localhost:8080/add/a/b
    //@RequestMapping("/add")//原来的
    //@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    @GetMapping("/add/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int res = a+b;
        model.addAttribute("msg","结果1为:"+res);
        return "test";
    }
    @PostMapping("/add")
    public String test2(@RequestParam String a, @RequestParam String b, Model model){
        String res = a+b;
        model.addAttribute("msg","结果2为:"+res);
        return "test";
    }
}

2、编写jsp页面【a.jsp】

<form action="add" method="post">
    <input type="text" name="a">
    <input type="text" name="b">
    <input type="submit">
form>

3、测试

springMVC-04-Controller配置总结和restful风格_第5张图片
springMVC-04-Controller配置总结和restful风格_第6张图片
springMVC-04-Controller配置总结和restful风格_第7张图片

相关资源: 学习狂神说 学习地址(以上仅为笔记).

你可能感兴趣的:(springMVC学习,restful,spring,java)