知識整理一:idea搭建maven 的springmvc項目(將maven項目轉變為web項目)


 

解釋:

maven項目穿件springMVC項目:這是個百度都寫爛了的項目筆記,哪里都有,這里寫出來也只是用於自己回顧。

正文:

工具:IDEA

1、轉換maven項目為web項目,如果已經是web項目,這一步省略

選擇一個maven項目,找到下面的面板之后--》選擇項目》點擊 + 彈出右側的Add面板,選擇 》Web

然后設置路徑

設置好路徑之后項目中就會自動創建一個web目錄。

2、添加pom.xml中對應的web包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dayu</groupId>
    <artifactId>tspring</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
        <jackson.version>2.9.8</jackson.version>
    </properties>
    <dependencies>
        <!--spring 核心包 start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--spring web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>


        <!--swagger2 整合springmvc-->
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>tspring</finalName>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <uriEncoding>utf-8</uriEncoding>
                    <path>/</path>
                    <port>80</port>
                    <!--<url>http://localhost:8888/manager/text</url>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

 

3、配置文件web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <!-- 在Spring框架中是如何解決從頁面傳來的字符串的編碼問題的呢?
  下面我們來看看Spring框架給我們提供過濾器CharacterEncodingFilter
   這個過濾器就是針對於每次瀏覽器請求進行過濾的,然后再其之上添加了父類沒有的功能即處理字符編碼。
    其中encoding用來設置編碼格式,forceEncoding用來設置是否理會 request.getCharacterEncoding()方法,設置為true則強制覆蓋之前的編碼格式。-->
  <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>
  <!-- 項目中使用Spring 時,applicationContext.xml配置文件中並沒有BeanFactory,要想在業務層中的class 文件中直接引用Spring容器管理的bean可通過以下方式-->
  <!--1、在web.xml配置監聽器ContextLoaderListener-->
  <!--ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置信息。因為它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啟動容器時,就會默認執行它實現的方法。
  在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。
  它的API說明
  第一段說明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。
  如果查看ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet這個接口
  第二段,ContextLoader創建的是 XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
  BeanFactory這樣一來spring中的所有bean都由這個類來創建
   IUploaddatafileManager uploadmanager = (IUploaddatafileManager)    ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");
   -->

  <!--2、部署applicationContext的xml文件-->
  <!--如果在web.xml中不寫任何參數配置信息,默認的路徑是"/WEB-INF/applicationContext.xml,
  在WEB-INF目錄下創建的xml文件的名稱必須是applicationContext.xml。
  如果是要自定義文件名可以在web.xml里加入contextConfigLocation這個context參數:
  在<param-value> </param-value>里指定相應的xml文件名,如果有多個xml文件,可以寫在一起並以“,”號分隔。
  也可以這樣applicationContext-*.xml采用通配符,比如這那個目錄下有applicationContext-ibatis-base.xml,
  applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都會一同被載入。
  在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。-->


  <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet,,所以可以配置多個DispatcherServlet-->
  <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(我們寫的Action)來處理。-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <!--在DispatcherServlet的初始化過程中,框架會在web應用的 WEB-INF文件夾下尋找名為[servlet-name]-servlet.xml 的配置文件,生成文件中定義的bean。-->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--指明了配置文件的文件名,不使用默認配置文件名,而使用dispatcher-servlet.xml配置文件。-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--其中<param-value>**.xml</param-value> 這里可以使用多種寫法-->
      <!--1、不寫,使用默認值:/WEB-INF/<servlet-name>-servlet.xml-->
      <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->
      <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->
      <!--4、多個值用逗號分隔-->
      <param-value>classpath:/spring*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup><!--是啟動順序,讓這個Servlet隨Servletp容器一起啟動。-->
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <!--Servlet攔截匹配規則可以自已定義,當映射為@RequestMapping("/user/add")時,為例,攔截哪種URL合適?-->
    <!--1、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會導致靜態文件(jpg,js,css)被攔截。-->
    <!--2、攔截/,例如:/user/add,可以實現現在很流行的REST風格。很多互聯網類型的應用很喜歡這種風格的URL。弊端:會導致靜態文件(jpg,js,css)被攔截后不能正常顯示。 -->
    <url-pattern>/</url-pattern> <!--會攔截URL中帶“/”的請求。-->
  </servlet-mapping>

</web-app>

 

 

 4、mvc的配置文件xml

spring-jdbc.xml 這個文件中需要注入mysql信息單獨寫在一個文件中

jdbc-class=com.mysql.jdbc.Driver
jdbc-user=username
jdbc-pwd=password
jdbc-url=jdbc:mysql://localhost:3306/chatroom?useUnicode=true&characterEncoding=utf-8

#useUnicode=true&characterEncoding=utf-8&serverTimeZone=GMT%2B8
minIdle=10
#時間ms
maxWait=60000 
connectionProperties=UTF-8
maxActive=20

 

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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">

    <context:property-placeholder location="classpath:*.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--類別名-->
        <property name="typeAliasesPackage" value="com.dayu.pojo"/>
        <!--映射文件位置,mapper.xml文件是創建在java包下,如果創建在resources包下則是classpath:/resources包下包名/*.xml-->
        <property name="mapperLocations" value="classpath:/com/dayu/mapper/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="factoryBean"/>
        <property name="basePackage" value="com.dayu.dao"/>
    </bean>

</beans>

 

spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
  <!--掃描controller -->
    <context:component-scan base-package="com.dayu.controller"/>
    <mvc:annotation-driven />

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

    <mvc:view-controller path="/show" view-name="show"/>
</beans>

spring-service.xml 事物控制都在這個文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:contxt="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <contxt:component-scan base-package="com.dayu.service.impl"/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

配置redis

redis.properties

redis.maxTotal=100
redis.maxIdl=10
redis.maxWaitMillis=3000
redis.blockWhenExhausted=true
redis.testOnBorrow=true

 

redis.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:redis.properties"/>

    <context:component-scan base-package="com.dayu.redis.dao"/>

    <bean id="hostAndPort1" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7001" />
    </bean>
    <bean id="hostAndPort2" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7002" />
    </bean>
    <bean id="hostAndPort3" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7003" />
    </bean>
    <bean id="hostAndPort4" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7004" />
    </bean>
    <bean id="hostAndPort5" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7005" />
    </bean>
    <bean id="hostAndPort6" class="redis.clients.jedis.HostAndPort">
        <constructor-arg name="host" value="192.168.120.111"/>
        <constructor-arg name="port" value="7006" />
    </bean>


    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxIdle" value="${redis.maxIdl}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg name="nodes">
            <set>
                <ref bean="hostAndPort1"/>
                <ref bean="hostAndPort2"/>
                <ref bean="hostAndPort3"/>
                <ref bean="hostAndPort4"/>
                <ref bean="hostAndPort5"/>
                <ref bean="hostAndPort6"/>
            </set>
        </constructor-arg>
        <constructor-arg name="timeout" value="6000"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
    </bean>

</beans>

 

 

 

 

 

項目路徑

 

 這樣整個配置就結束了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM