01 基于注解的springmvc的第一个demo

本文将阐述基于注解的springmvc的第一个入门demo。

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • jdk-8u162-windows-x64
  • spring4.2.4

前提约束

  • 完成创建一个基于maven的web工程

操作步骤

  • 在pom.xml中加入以下依赖和插件
        
        4.2.4.RELEASE
        1.2.16
    

    
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            log4j
            log4j
            ${log4j.version}
        
    
    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                
                    /
                    8080
                
            
        
    
  • 在src/main/resoures文件夹下加入spring-mvc.xml文件
    
    

         
    
  • 修改src/main/webapp/WEB-INF/web.xml内容


    
        mvc
        /
    
    
        mvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    

  • 在src/main/java文件夹下加入net.wanho.controller.UserController.java
package net.wanho.controller;

import net.wanho.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    @RequestMapping(value = "/user/query",method = RequestMethod.GET)
    @ResponseBody
    public String query()
    {
        return "helloworld";
    }
}
  • 启动tomcat插件,测试
    在本地浏览器中输入"http://localhost:8088/user/query",回车,则在页面看到"helloworld"字符串。

你可能感兴趣的:(01 基于注解的springmvc的第一个demo)