七、初学SpringMVC+Mybatis之SpringMVC基于注解的配置

1、搭建环境


2、在web.xml中配置DispatcherServlet 前端控制器组件

以上两部是配置spring容器,基于注解的配置和基于xml的配置都一样,具体步骤见上一篇


3、编写Controller组件

package pers.zky.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * @author Zky
 * @version 1.0
 * Controller代替了helloController的bean配置
 *      <bean id="helloController" class="pers.zky.controller.HelloController"></bean>
 *      并省了继承org.springframework.web.servlet.mvc.Controller接口
 * RequestMapping代替了handlerMapping的配置
 *      <prop key="/hello/hello.do">helloController</prop>
 */
@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/first.do")
    public ModelAndView hello1(){
        return new ModelAndView("first");
    }
    @RequestMapping("/second.do")
    public ModelAndView hello2(){
    return new ModelAndView("second");
    }
}

4、applicationContext中开启注解扫描

<context:component-scan base-package="pers.zky"></context:component-scan>

5、applicationContext中开启SpringMVC的扫描驱动

<mvc:annotation-driven></mvc:annotation-driven>

6、 applicationContext中配置视图解析器

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


5、新建jsp页面,完成跳转的功能

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值