springMVC的xml配置

构建过程

项目结构如图


springMVC的xml配置_第1张图片
image.png

pom.xml,添加springmvc相关依赖



    4.0.0

    com.haijunyin
    springmvcdemo-xmlconfig
    1.0-SNAPSHOT

    war

    
        
            org.springframework
            spring-web
            4.3.8.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.3.8.RELEASE
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
    

hello.html



    
        
    
    
        

HelloController.class

package com.haijunyin.springmvcdemo.xmlconfig.controller;

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.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/hello")
public class HelloController {

    //返回String类型代表页面跳转
    @RequestMapping(value = "/ref",method = RequestMethod.GET)
    public String refHello(){
        System.out.println("HelloController...refHello...");
        return "hello";
    }

    //使用RequestParam注解直接获取值
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    public void showHello(@RequestParam(value = "name")String name, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ShowHelloServlet...doPost...");
        //获取值,并设置编码
        System.out.println("name=" + name);
        //输出,解决浏览器乱码问题
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write("我很好,你呢?");
        System.out.println("输出...");
    }

}

index.jsp

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


    
    
        hello
    

web.xml



    web02
    
    
        index.jsp
    

    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        springmvc
        /
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    

     

springmvc-servlet.xml,这里的命名与web.xml中spring配置的servletName相对应




    
    

    
    

    
    

    
    
        
        
        
        
    

运行结果

项目构建完成后启动tomcat

springMVC的xml配置_第2张图片
1547104625527.gif

你可能感兴趣的:(springMVC的xml配置)