/----------关键字:springmvc注解


M1:这篇内容是在上一篇的基础上进行的修改。具体步骤如下:

M2:新建项目Dynamic Web Project 项目annotation

M3:其它地方不变只需要对springmvc_config,xml 和HelloController进行修改

M4:springmvc_config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        <!-- spring扫描对应包下面的注解,扫描到注册为spring的bean -->
         <context:component-scan base-package="com.mollen.controller"/>  
          <!-- 配置annotation类型的处理映射器 -->
      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
          <!-- 配置annotation类型的处理器适配器 -->
      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
          <!-- 视图解析器 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>       
        
</beans>

M5:HelloController修改如下

package com.mollen.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * HelloCotroller是一个基于注解的控制器
 * 可以同时处理多个请求,并且不用实现任何接口
 * org.springframework.stereotype.Controller注解用于只是该类是一个控制器
 */
@Controller
public class HelloCotroller {
    private static final Log log = LogFactory.getLog(HelloCotroller.class);
    
    /**
     * org.springframework.web.bind.annotation.RequestMapping注解
     * 用来映射请求的url和请求的方法,本例用来映射"/hello"
     * hello只是一个简单的方法
     * 该方法返回一个视图名或视图名和模型对象
     */
    @RequestMapping(value="/hello")
    public ModelAndView hello(){
        //打印日志
        log.info("HelloController 被调用");
        //创建视图模型,该对象包含视图名,模型名称,模型对象
        ModelAndView mv = new ModelAndView();
        //添加模型数据,可以是pojo对象
        mv.addObject("message", "hello world.");
        //视图解析器根据名字将数据解析到指定url页面
        mv.setViewName("/WEB-INF/view/welcome.jsp");
        return mv;
    }
}

M6:部署并访问

优质内容筛选与推荐>>
1、Javascript中Array.prototype.map()详解
2、【刷题】BZOJ 1453 [Wc]Dface双面棋盘
3、搭建自己专属的vpn——Centos搭建vpn的几种办法
4、梦断代码读后感01
5、eclipse link方式安装插件安装不上


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn