SpringMVC第一个入门例子

这是我第一次使用SpringMVC框架的第一个例子

项目结构:

SpringMVC第一个入门例子_第1张图片

导入springmvc包

配置web.xml文件



  SpringMVC01
  
     
  
  	encodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  
  
  	encodingFilter
  	/*
  
  
	
   		 dispatcherServlet
   		 	
    		org.springframework.web.servlet.DispatcherServlet
	  			
	  				
		  			contextConfigLocation
		  			/WEB-INF/mvc-config.xml
	  			
  			
  		1
  
  
    	dispatcherServlet
    	
    	*.do
	


配置springmvc配置文件


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
         
         



  

编写控制类HelloController.java

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("-------hello world--------");//会在控制台打印hello world
		return new ModelAndView("welcome");//并跳转到welcome这个jsp页面
	}

}

在WEB-INF下建一个jsp文件夹,在jsp文件夹下建welcome.jsp页面


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




Insert title here


	hello...


现在就可以测试了

SpringMVC第一个入门例子_第2张图片



你可能感兴趣的:(spring入门)