springMVC项目创建

结合maven一起使用,首先通过File-->new-->Maven Project创建maven项目,springMVC项目创建_第1张图片

springMVC项目创建_第2张图片

springMVC项目创建_第3张图片

点击Finish完成创建。

创建后目录结构如下:

springMVC项目创建_第4张图片

pom.xml中存储的是maven依赖,从而导入相关jar包。

在src中建立如下目录结构:

springMVC项目创建_第5张图片

pom.xml文件内容如下:


  4.0.0
  QC2
  learn_01
  war
  0.0.1-SNAPSHOT
  learn_01 Maven Webapp
  http://maven.apache.org
  
  
     
        
		4.3.8.RELEASE
		UTF-8
     
  
  
  
  
 	   mysql
  	  mysql-connector-java
  	  6.0.6
	

    	
			junit
			junit
			3.8.1
			test
		
		
			javax.servlet
			javax.servlet-api
			3.0.1
			provided
		
		        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        

        
        
            org.aspectj
            aspectjrt
            1.8.0
        
        
            org.aspectj
            aspectjweaver
            1.8.0
        

        
        
            org.mybatis
            mybatis
            3.3.0
        
        
        
            mysql
            mysql-connector-java
            5.1.26
        
        
        
            com.alibaba
            druid
            1.0.20
        

        
        
            org.mybatis
            mybatis-spring
            1.2.3
        

        
        
            javax.servlet
            jstl
            1.2
            compile 
        
		
		
		    com.google.code.gson
		    gson
		    2.8.5
		
        
        
            log4j
            log4j
            1.2.17
        
        
            org.slf4j
            slf4j-api
            1.7.21
        
		
		
		
	
		
			org.apache.poi
			poi
			3.8
			
				
					commons-codec
					commons-codec
				
			
		
		
			org.apache.poi
			poi-ooxml
			3.8
		
		
		 
            commons-fileupload
            commons-fileupload
            1.2.1
        
        
        
            commons-io
            commons-io
            2.4
        
        
        
		    org.springframework
		    spring-context-support
		    5.0.0.RELEASE
		
		
		
		      com.sun.mail
		      javax.mail
		      1.6.1
		
  
  
    learn_01
  

web.xml内容如下:



     Archetype Created Web Application
  
    contextConfigLocation
    classpath:spring/spring-*.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    Dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring/spring-mvc.xml
    
    1
  
  
    Dispatcher
    /
  
  
    index.jsp
  

spring-mvc.xml如下:



	
	

	
	

	
	

	
	
		
		
	

	
		
		
		
	

	

创建TestController.java如下:

package com.qc2.controllers;

import java.util.List;

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 org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

@Controller
public class TestContorller {
	@ResponseBody
    @RequestMapping(value ="views/hello", produces = "application/json;charset=utf-8",method ={RequestMethod.GET})
	public String hello(String age){        
        
        Gson gson =new Gson();
        String json =gson.toJson(age);
		return json;
    }
	
	
    @RequestMapping(value ="views/hello2", produces = "application/json;charset=utf-8",method ={RequestMethod.GET})
    public ModelAndView hello2(String name){ 
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("message","ooooooooooooooo");
    	mv.setViewName("h2");
        return mv;
    }
}

创建Test.jsp如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    





Insert title here


ss

运行结果如下:

springMVC项目创建_第6张图片

你可能感兴趣的:(Java)