(不推荐使用)springMVC基本配置+controller层实现Controller接口

基本配置:
web.xml

 <!-- 配置springMVC -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
         <!-- contextConfigLocation 是固定的 -->
            <param-name>contextConfigLocation</param-name>
                 <!-- 配置读取路径-->
            <param-value>classpath*:config/spring-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>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!--配置url映射的类  -->
    <bean name="/test/hello" class="com.tgb.web.controller.HelloWorld" />
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 </beans>  

controller

//实现Controller接口
public class HelloWorld implements Controller {
   //重写handleRequest方法,spring-servlet.xml配置的映射默认会调用该重写的方法
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {


        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", "丽华");
        map.put("password", "123");

        return new ModelAndView("/welcome","map",map);


    }


}

jsp

    <div>
        <c:forEach items="${map }" var="m">
        ${m.key } ---- ${m.value }
        <br>
        </c:forEach>
    </div>

结果:

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值