SpringMVC 环境搭建

SpringMVC 环境搭建:

1、创建 webapp 的模型的 Maven 项目

2、在 Maven 文件中导入依赖

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.2.8.RELEASEversion>
dependency>

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.12version>
dependency>

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>servlet-apiartifactId>
    <version>2.5version>
dependency>

3、在 web.xml 文件中配置前置处理器DispatcherServlet),并且配置 springmvc.xml 文件

<servlet>
    
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
        
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
servlet>


<servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
servlet-mapping>


<servlet-mapping>
    <servlet-name>defaultservlet-name>
    <url-pattern>*.jsurl-pattern>
servlet-mapping>

4、创建 springmvc.xml 配置文件, 配置视图解析器ViewResolver),配置包扫描,消息转换器…


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven>
        
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8">property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

    <context:component-scan base-package="com.cl">context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/">property>
        <property name="suffix" value=".jsp">property>
    bean>
beans>

5、编写业务接口

@Controller
public class HelloHandler {
    
    @RequestMapping(value = "/index", method = RequestMethod.GET, params = {"name","id"})
    public String index(@RequestParam("name") String name,@RequestParam("id") int id){
        System.out.println("执行了 index 方法...");
        System.out.println("name:  "+name+"    id:   "+id);
        return "index";
    }

    // restful风格请求
    @RequestMapping(value = "/restful/{name}/{id}",method = RequestMethod.GET)
    public String indexRest(@PathVariable("name") String name,@PathVariable("id") int id){
        System.out.println("执行了 indexRest 方法...");
        System.out.println("name:  "+name+"    id:   "+id);
        return "index";
    }


    @RequestMapping("/json")
    public User json(@RequestBody User user, HttpServletResponse response){
        response.setCharacterEncoding("text/json;charset=UTF-8");
        System.out.println(user);
        user.setId(6L);
        user.setName("张六");
        return user;
    }
}

6、浏览器访问接口测试

你可能感兴趣的:(Spring,全家桶,SpringMVC)