zoukankan      html  css  js  c++  java
  • [java]快速创建servlet和springmvc测试项目

    web三层架构

    web层
        com.demo.
            web
            servlet
            controller
    service层
        com.demo.
            service
                serviceimpl
    dao层
        com.demo.
            dao
                daoimpl
            repository
            mapper
    实体bean对象
        com.demo.
            pojo
            entry
            domain
            bean
            model
    测试包
        com.demo.
            test
            junit
    工具类
        com.demo.
            utils
    

    参考
    新建一个Maven工程当做父工程!(删除无关的)可以不用使用任何模板. 子项目可以继承父的依赖.

    创建springmvc项目

    new project, maven webapp模板

    pom.xml

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
    

    如果需要事务支持

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>5.2.5.RELEASE</version>
            </dependency>
    

    如果还需要servlet开发, 导入相关包即可.

    添加依赖后idea刷新maven

    创建对应的包(目录)

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <display-name>Archetype Created Web Application</display-name>
        <!-- 字符编码处理 -->
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
           
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        <!--    支持form形式restful-->
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <servlet-name>DispatcherServlet</servlet-name>
        </filter-mapping>
    
        
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>DispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
           xmlns="http://www.springframework.org/schema/beans"
           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">
        <!-- scan the package and the sub package -->
        <context:component-scan base-package="com.demo"></context:component-scan>
    
        <!-- configure the InternalResourceViewResolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!-- don't handle the static resource -->
        <mvc:default-servlet-handler/>
        <!-- if you use annotation you must configure following setting -->
        <mvc:annotation-driven/>
    </beans>
    

    【Spring框架】mvc:default-servlet-handler/的作用

    <mvc:default-servlet-handler />
    
    <mvc:resources location="/,classpath:/META-INF/publicResources/" mapping="/resources/**"/>
    

    Content type 'application/json;charset=UTF-8' not supported解决:添加 mvc:annotation-driven/

    415 (Unsupported Media Type) 原因及解决方案: 前端缺了content-type为json

    springmvc controller层接收List类型的参数

    接收List、List、List<Map<String,Object>>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例

    controller

    HelloWorld.java

    @Controller
    public class HelloWorld {
        @RequestMapping("/test01")
        @ResponseBody
        public String index() {
            return "test";
        }
    }
    

    HelloWorld.java

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloWorld {
        @GetMapping("/test01")
        public String testget() {
            return "do get";
        }
    
        @PostMapping("/test01")
        public String testpost() {
            return "do post";
        }
    }
    

    添加tomcat服务器

    启动tomcat服务测试

    创建servlet测试模块

    父模块选择刚创建的

    <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
       <-- 导入springmvc依赖 !->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>5.1.9.RELEASE</version>
       </dependency>
       <-- 导入servlet 和 jsp 的 jar 依赖 !->
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
           <version>2.5</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet.jsp</groupId>
           <artifactId>jsp-api</artifactId>
           <version>2.2</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
    </dependencies>
    

    web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
            version="4.0">
       <servlet>
           <servlet-name>HelloServlet</servlet-name>
           <servlet-class>com.demo.servlet.HelloServlet</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>HelloServlet</servlet-name>
           <url-pattern>/user</url-pattern>
       </servlet-mapping>
    </web-app>
    
  • 相关阅读:
    2星|《丰田一页纸极简思考法》:僵化、不易扩展、修改,价值不大
    4星|《亿万》:FBI大战华尔街对冲基金大鳄
    3星|《CMO到底能干多久?》:CEO必须决定供公司需要哪类CMO
    中老年创业成功率更高,美国投资人的创业指南:4星|《烧掉你的商业计划书》
    3星|《科技投资新时代》:TMT行业资讯汇编
    2星|史蒂芬·平克《风格感觉》:英语写作指导,不适合外行阅读
    3星|《故事课1》:7个步骤+36种情节+悬念,作者没有拿得出手的故事作品
    2.5星|《数字货币:世界为我疯狂》:5年来《商业周刊》的区块链与比特币相关的文章16篇
    2.5星|《哈佛商学院管理与MBA案例全书》:书名太唬人了,依据中文经管书汇编整理而成
    3.5星|《第一本经济学》:通俗、可信的经济学入门
  • 原文地址:https://www.cnblogs.com/iiiiiher/p/12721237.html
Copyright © 2011-2022 走看看