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>
    
  • 相关阅读:
    Linux(8)查看服务器系统信息
    Scrapy爬取西刺代理ip流程
    Scrapy提取多个标签的text
    Scrapy常用命令行工具
    暴力&打表
    博客试水
    Linux --远程访问控制
    php--最新正则(手机号码)
    MySQLdump常用命令
    关于XSS攻击
  • 原文地址:https://www.cnblogs.com/iiiiiher/p/12721237.html
Copyright © 2011-2022 走看看