eclipse下spring web的搭建过程

1: 采用maven进行版本管理,所以首先建立maven项目
文件-新建-maven project-artifact id 选择webapp-输入group Id 和artface Id - 点击finish
生成完成之后如下所示:


eclipse下spring web的搭建过程_第1张图片
image.png

2: 添加运行时的tomcat服务器
在项目上右键-build path- configure build path-选择library标签-选择右侧的add library - server runtime - 选择安装的tomcat 服务器--finish,然后apply一下即可。
3: 将需要的依赖包加入pom 文件
4: 在resources文件夹下新建/META-INF/spring-mvc.xml文件






    
        
              
           
    





5: 在src/webapp/WEB-INF/web.xml中添加配置文件

  
 
    Archetype Created Web Application
    
    
        index.jsp
    
    
    
        spring
        org.springframework.web.servlet.DispatcherServlet
        
          contextConfigLocation
          
          classpath:META-INF/spring-mvc.xml
        
        
        1
    
    
        spring
        /
    

6: 在src/java下新建com.test.controller文件夹,然后新建controller文件如下所示

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(){
        System.out.println("I am stupid ok ok oko ko ko ko ko ko k ok o ko ko kok o");
//        实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀
        return "test";
    }
}

7: 在src/main/webapp/WEB-INF下新建views文件夹作为我们的目标模版的存放位置,然后新建一个test.jsp

package com.zhaolong.wsn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(){
        System.out.println("I am stupid ok ok oko ko ko ko ko ko k ok o ko ko kok o");
//        实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀
        return "test";
    }
}

8: 在项目上右键run as -- run on server 启动之后 在原始地址后面输入test回车就可以看到新建的test页面了。

你可能感兴趣的:(eclipse下spring web的搭建过程)