• springmvc基础篇—通过注解的方式去配置项目


    学习了通过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/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                            http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-3.2.xsd">
        
        <!-- 自动扫描 -->
        <context:component-scan base-package="cn.cfs.springmvc.controller"/>
        <!-- 内部资源视图解析器 prefix + logicName + suffix -->
        <bean name="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsps/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!-- 注解驱动 -->
        <mvc:annotation-driven/>
    </beans>
    spring-mvc.xml

    注意:自动扫描配置中,如果有多个包要扫描,可以用逗号隔开

    二、示例:

    package cn.cfs.springmvc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HomeController{
    
        @RequestMapping(value={"/","home"})
        public String test(){
            System.out.println("进来了......");
            return "index";
        }
        
    }

    注意:@RequestMapping中value使用{}来括起来了有点类似我们的数组,所以如果是想用多个不同的url去访问那么直接像我这么写就好了,我在做这个示例的时候出现了一点问题,发现配置都没问题,但是一访问就是抛错,在网上查阅资料后,发现原来注解方式需要用到的jar包需要是4.0以上才行,有的人用3.x.x的版本也可以,但是我这就不行,没有去深入入研究,可能是和tomcat版本有关系,所以建议大家用spring4.0以上的jar文件。

    我这里贴出一个从官网下载的jar包分享地址:http://pan.baidu.com/s/1pKUr8BD

    三、创建jsp文件,访问截图

    在你配置内部资源视图解析器中,别忘了去建对应jsp文件,然后就可以访问了

  • 相关阅读:
    Entity Framework开源了
    动态执行超过4000个字符的SQL
    微软针对Access提供了免费的SQL Server移植工具——SSMA
    网络采集库NCrawler
    使用WinSCP软件在windows和Linux中进行文件传输
    Open Source C# (Mono Compatible) Library for Sending Push Notifications To iOS (iPhone/iPad APNS), Android (C2DM), Windows Phone Apps
    Mono 2.11.3 发布包含微软的 Entity Framework
    谷歌发布 AngularJS 1.0,允许扩展HTML语法
    Accord.NET Framework
    CentOS源码编译安装Nginx和tcp_proxy module
  • 原文地址:https://www.cnblogs.com/mycifeng/p/6367157.html
Copyright © 2020-2023  润新知