SpringMVC-03-Controller

SpringMVC-03-Controller

  • 控制器负责提供访问应用程序的行为,通常通过接口定义或者注解定义两种方式实现
  • 控制器负责解析用户的请求并将其转换为一个模型
  • 在SpringMVC中一个控制器可以包含多个方法
  • 在SpringMVC中,对于Controller的配置方式有多种

使用接口实现

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--视图解析器-->
    <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--使用接口的方式,必须注册bean-->
    <bean id="/t" class="com.cmy.controller.ControllerDemo01"/>
</beans>

controller类 实现Controller接口

public class ControllerDemo01 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","hello guys!");
        mv.setViewName("/test");
        return mv;
    }
}

改方法有明显的确定,一个控制器只有一个方法,如果需要多个方法,则需要同等数量的控制器,这无疑是繁琐的

使用注解@Controller实现

  • @Componet 组件
  • @Service service层
  • @Controller controller层
  • @Respository dao层

配置springmvc-servlet.xml

<context:component-scan base-package="com.cmy.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
@Controller
//代表这个类被Spring托管
//被改注解注解的类中的所有方法,如果返回值是String,且有与之同名的页面存在,则会被视图解析器解析
public class ControllerDemo02 {
    @RequestMapping("t2")
    public String test01(Model model){
        model.addAttribute("msg","hello guys2");
        return "test";//WEB/INF/jsp/hello.jsp
    }

    @RequestMapping("t3")
    public String test02(Model model){
        model.addAttribute("msg","hello guys3");
        return "test";//WEB/INF/jsp/hello.jsp
    }
}

视图可以被复用,根据不同的方法返回不同的信息,视图确是同一个test.jsp,视图和控制器是弱耦合关系

补充

@RequestMapping

指定前端路径访问路径,并映射至返回的视图

如上面的例子在浏览器访问http://127.0.0.1:8080/t2通过视图解析器映射到WEB/INF/jsp/test.jsp

这里要注意@RequestMapping如果加在类上,则路径访问时要加一层

@Controller
@RequestMapping("/father")
public class ControllerDemo02 {
    @RequestMapping("/son")
    public String test01(Model model){
        model.addAttribute("msg","hello guys2");
        return "test";//WEB/INF/jsp/hello.jsp
    }
}    

http://127.0.0.1:8080/father/son

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值