多种实现 spring - mvc 开发方式(注解 , 实现接口)

注解开发MVC
1.配置web.xml
<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个springmvc的配置文件:[servlet].xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动级别1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- / 匹配所有请求 : 不包括(jsp)-->
    <!-- /* 匹配所有的请求 : 包括jsp-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
2.配置spring
<?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">
   <!--自动扫描包,让指定包下的注解生效 ,用IOC容器管理 -->
   <context:component-scan base-package="com.sl.contorller"/>

    <!--让Spring 不处理静态资源 .css .js ...-->
    <mvc:default-servlet-handler />


    <!--
    支持mvc注解驱动
       在spring中- -般 采用@Reques tMapping注解来完成映射关系
       要想使@RequestMapping注解生效
       必须向上下文中注册Defaul tAnnotationHandlerMapping
       和一- TAnnotationMe thodHandlerAdapter实例
       这两个实例分别在类级别和方法级别处理。
       而jannotation-driven配置帮助我们自动完成上述两个实例的注入。
      -->
   <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>
</beans> 
3.使用注解
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*代表 这类会被Spring 接管,被这个注解的类,重            的所有方法 如果返回的时String ,
并且 有具体页面可以选择,那么就ibei视图解析器 解析:
*/
@Controller 
public class HelloContorller {
    @RequestMapping
    public String hello(Model model){
        //封装数据
        model.addAttribute("msg","HelloSpringMVCAnnotation");
        //视图的名称
        return "hello"; //会被视图解析器处理
    }
}

实现接口 MVC开发

(不建议使用)

1.实现contorller
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//实现类Contorller 接口的类 ,说明这就是一个控制器
public class HelloContorller2 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        //创建视图模型
        ModelAndView mv = new ModelAndView();
        //传递数据
        mv.addObject("msg","holloMVC");
        //跳转视图
         mv.setViewName("hello");
        return mv;
    }
}
2. web配置同上
3.spring配置
<?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 class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
     </bean>
    <bean id="/hello" class="com.sl.contorller.HelloContorller2"/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值