【SpringMVC】​SpringMVC组件、视图、组件、视图解析、页面跳转

一、组件解析分析

  1. DispatcherServlet:前端控制器,整个流程的控制中心,调用其他组件处理客户端请求,它降低了代码的耦合性。
  2. HandlerMapping:处理器映射器,根据用户的请求找到一个合适的处理器(Handler)。
  3. HandlerAdaptor:处理器适配器,通过HanderAdaptor对处理器进行执行适配并应用。
  4. viewResolver:视图解析器,负责处理生成视图。

 


二、视图映射

  1. 默认转发是一次请求;重定向是两次请求。
  2. 方法返回的视图时转发(forward)。
@Controller
@RequestMapping(value = "index")
public class IndexController {
    @RequestMapping(value = "toIndex")
    public String toIndex(){
        return "redirct:/index.jsp";
    }
}

三、组件扫描

<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.cxsw.controller"/>

</beans>

四、视图解析

  1. prefix:前缀。
  2. suffix:后缀。
  3. WEB-INF下的目录和文件是受保护的。
<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.cxsw.controller"/>

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

五、页面跳转

@Controller
@RequestMapping(value = "index")
public class IndexController {
    @RequestMapping(value = "toIndex")
    public String toIndex(){
        return "index";
    }

    @RequestMapping(value = "redirectIndex")
    public String toRedirect(){
        return "redirect:/index/toIndex";
    }

    @RequestMapping("toModelAndView")
    public ModelAndView toModelAndView(){
        ModelAndView view = new ModelAndView();
        view.setViewName("index");
        view.addObject("username", "张三");
        return view;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小韩要努力。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值