從0開(kāi)始創(chuàng)建環(huán)境(spring mvc)

2022-09-16 09:37 更新

1.創(chuàng)建web項(xiàng)目

2.添加maven支持

3.修改pom.xml

<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>
     
    <parent>
        <groupId>org.anyline</groupId>
        <artifactId>anyline-maven-mvc</artifactId>
        <version>1.0.8</version>
    </parent>
     
    <artifactId>anyline-start</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <name>anyline-start</name>
</project>

以下是:anyline-maven-mvc-1.0.8內(nèi)容

<?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>org.anyline</groupId>
    <artifactId>anyline-maven-mvc</artifactId>
    <version>1.0.8</version>
    <packaging>pom</packaging>
    <name>anyline-maven-mvc</name>
    <url>http://www.anyline.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
 
        <anyline.version>8.0.8</anyline.version>
        <anyline.core.version>8.0.8</anyline.core.version>
        <anyline.ext.version>8.0.8</anyline.ext.version>
        <anyline.web.version>8.0.8</anyline.web.version>
        <anyline.web.ext.version>8.0.8</anyline.web.ext.version>
        <anyline.springmvc.version>8.0.8</anyline.springmvc.version>
 
        <anyline.amap.version>8.0.8</anyline.amap.version>
        <anyline.alipay.version>8.0.8</anyline.alipay.version>
        <anyline.aliyun.version>8.0.8</anyline.aliyun.version>
        <anyline.easemob.version>8.0.8</anyline.easemob.version>
        <anyline.jpush.version>8.0.8</anyline.jpush.version>
        <anyline.mail.version>8.0.8</anyline.mail.version>
        <anyline.mssql.version>8.0.8</anyline.mssql.version>
        <anyline.mysql.version>8.0.8</anyline.mysql.version>
        <anyline.qq.version>8.0.8</anyline.qq.version>
        <anyline.redis.version>8.0.8</anyline.redis.version>
        <anyline.weixin.version>8.0.8</anyline.weixin.version>
 
        <javax.servlet.version>4.0.0</javax.servlet.version>
        <javax.jsp.version>2.0</javax.jsp.version>
        <javax.jstl.version>1.1.2</javax.jstl.version>
        <taglibs.standard.version>1.1.2</taglibs.standard.version>
        <javax.mail.version>1.5.0-b01</javax.mail.version>
 
        <log4j.version>1.2.17</log4j.version>
        <json.version>2.4</json.version>
        <dom4j.version>2.1.1</dom4j.version>
        <commons.net.version>3.6</commons.net.version>
        <commons.io.version>2.6</commons.io.version>
        <commons.httpclient.version>3.1</commons.httpclient.version>
        <apache.httpcomponents.version>4.5</apache.httpcomponents.version>
        <apache.struts.version>2.3.24.1</apache.struts.version>
        <oro.version>2.0.8</oro.version>
        <ecache.version>2.10.6</ecache.version>
        <ognl.version>3.2.10</ognl.version>
        <slf4j.version>1.7.2</slf4j.version>
        <mssql.jdbc.version>7.2.2.jre8</mssql.jdbc.version>
        <mysql.jdbc.version>5.1.24</mysql.jdbc.version>
 
        <spring.version>4.3.23.RELEASE</spring.version>
 
        <aliyun.oss.version>3.3.0</aliyun.oss.version>
        <aliyun.sms.version>1.1.0</aliyun.sms.version>
        <alipay.version>3.7.26.ALL</alipay.version>
        <druid.version>1.1.6</druid.version>
        <jsch.version>0.1.55</jsch.version>
        <persistence.version>1.0.2</persistence.version>
 
    </properties>
 
    <dependencies>
        <!-- 連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.jdbc.version}</version>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${javax.jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>${taglibs.standard.version}</version>
        </dependency>
        <!-- springmvc支持 -->
        <dependency>
            <groupId>org.anyline</groupId>
            <artifactId>anyline-mvc</artifactId>
            <version>${anyline.version}</version>
        </dependency>
        <!-- mysql支持 -->
        <dependency>
            <groupId>org.anyline</groupId>
            <artifactId>anyline-mysql</artifactId>
            <version>${anyline.version}</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.tld</include>
                </includes>
                <targetPath>META-INF</targetPath>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <excludes>
                            <excludes>*.xml</excludes>
                            <excludes>*.tld</excludes>
                            <excludes>*.xsd</excludes>
                            <excludes>*.properties</excludes>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.maven.plugins
                                        </groupId>
                                        <artifactId>
                                            maven-resources-plugin
                                        </artifactId>
                                        <versionRange>
                                            [2.5,)
                                        </versionRange>
                                        <goals>
                                            <goal>resources</goal>
                                            <goal>testResources</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

4.修改WEB-INF/web.xml添加springmvc支持

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance  http://www.springmodules.org/schema/cache/springmodules-cache.xsd http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    <display-name>springmvc</display-name>
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.txt</url-pattern>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.gif</url-pattern>
        <url-pattern>*.jpg</url-pattern>
        <url-pattern>*.jpeg</url-pattern>
        <url-pattern>*.ico</url-pattern>
        <url-pattern>*.png</url-pattern>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <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>
    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>
</web-app>

5.修改springmvc配置文件添加anyline支持

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="org.anyline"></context:component-scan>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.anyline.plugin.springmvc.HttpCharsetConvert" />
        </mvc:message-converters>
    </mvc:annotation-driven>
    <bean id="viewResolver" class="org.anyline.plugin.springmvc.TemplateResourceViewResolver">
        <property name="viewClass" value="org.anyline.plugin.springmvc.TemplateView" />
        <property name="suffix" value=".jsp"></property>
    </bean>
     
    <mvc:default-servlet-handler />
    <bean id="ds_base" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="5" />
        <property name="minIdle" value="20" />
        <property name="maxActive" value="100" />
 
        <!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
        <property name="maxWait" value="60000" />
 
        <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
 
        <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
 
        <property name="validationQuery" value="SELECT 0" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
 
        <!-- 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
 
        <!-- 對(duì)于長(zhǎng)時(shí)間不使用的連接強(qiáng)制關(guān)閉 -->
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="120" />
         
        <!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters -->
        <property name="filters" value="stat" />
    </bean>
    <bean id="ds_def" parent="ds_base" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本屬性 url、user、password -->
        <property name="url" value="jdbc:mysql://127.0.0.1:36902/anyline_start?useUnicode=true&amp;characterEncoding=UTF8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
     
    <bean id="ds" class="org.anyline.config.db.ds.DynamicDataSource">  
        <property name="targetDataSources">  
            <map key-type="java.lang.String">
                <!-- 指定lookupKey和與之對(duì)應(yīng)的數(shù)據(jù)源 -->
                <entry key="ds_def" value-ref="ds_def"></entry>  
            </map>  
        </property>  
        <!-- 這里可以指定默認(rèn)的數(shù)據(jù)源 -->
        <property name="defaultTargetDataSource" ref="ds_def" />  
    </bean>
    <!-- JDBC模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds" />
    </bean>
     
    <!-- Initialization for TransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds" />
    </bean>
     
</beans>

6.添加anyline-config.xml配置文件,包含了一些常用的配置參數(shù)

<?xml version="1.0" encoding="UTF-8"?> 
<!-- DTD for properties -->
<!DOCTYPE properties [
    <!ELEMENT properties ( comment?, property* ) >
    <!ATTLIST properties version CDATA #FIXED "1.0">
    <!ELEMENT comment (#PCDATA) >
    <!ELEMENT property (#PCDATA) >
    <!ATTLIST property key CDATA #REQUIRED>
]>
<properties>
    <!-- 多長(zhǎng)時(shí)間加載一次配置文件 (秒)-->
    <property key="RELOAD">120</property>
    <!-- 存放在servlet context中的key(配置文件熱重載時(shí)不覆蓋當(dāng)前key)(如el表達(dá)式中引用${al.RELOAD})-->
    <property key="SERVLET_ATTRIBUTE_KEY">al</property>
    <property key="DEBUG">true</property>
    <!-- sql 存放根目錄 -->
    <property key="SQL_STORE_DIR">/WEB-INF/classes/sql</property>
    <!-- 密鑰 文件路徑 -->
    <property key="DES_KEY_FILE">/WEB-INF/classes/key.xml</property>
    <!-- 國(guó)際化文本存放目錄 -->
    <property key="I18N_MESSAGE_DIR">/WEB-INF/classes/message</property>
    <!-- 國(guó)際化消息SESSION存放KEY -->
    <property key="I18N_MESSAGE_SESSION_KEY">I18N_MESSAGE_SESSION_KEY</property>
    <!-- 國(guó)際化消息默認(rèn)語(yǔ)言 -->
    <property key="I18N_MESSAGE_DEFAULT_LANG">cn</property>
    <!-- 默認(rèn)主鍵 -->
    <property key="DEFAULT_PRIMARY_KEY">ID</property>
    <!-- 自動(dòng)添加主鍵 -->
    <property key="AUTO_CREATE_PRIMARY_KEY">true</property>
    <!-- 是否顯示執(zhí)行的SQL -->
    <property key="SHOW_SQL">true</property>
    <property key="SHOW_SQL_PARAM">true</property>
    <property key="SHOW_SQL_WHEN_ERROR">false</property>
    <property key="SHOW_SQL_PARAM_WHEN_ERROR">false</property>
     
 
    <!-- 登錄驗(yàn)證碼圖片寬度 -->
    <property key="VALIDATE_CODE_IMG_WIDTH">60</property>
    <!-- 登錄驗(yàn)證碼圖片寬度 -->
    <property key="VALIDATE_CODE_IMG_HEIGHT">20</property>
    <!-- 登錄驗(yàn)證碼圖片 字符個(gè)數(shù) -->
    <property key="VALIDATE_CODE_IMG_CHAR_SIZE">4</property>
    <!-- 登錄驗(yàn)證碼圖片 復(fù)雜度 -->
    <property key="VALIDATE_CODE_IMG_COMPLEXITY">10</property>
     
    <!-- 是否更新NULL列 -->
    <property key="IS_UPDATE_NULL_COLUMN">false</property>
    <!-- 是否更新空列 -->
    <property key="IS_UPDATE_EMPTY_COLUMN">false</property>
     
    <!-- 生成主鍵的存儲(chǔ)過(guò)程 按3個(gè)輸入?yún)?shù)的格式:表,列,其他-->
    <property key="AUTO_CREATE_PRIMARY_KEY">true</property>
    <property key="CREATE_PRIMARY_KEY_PROCEDURE">PROC_CREATE_PRIMARY</property>
     
    <!-- 模板文件目錄 -->
    <property key="TEMPLET_FILE_PATH_WEB">/WEB-INF/web/home/template/default.jsp</property>
    <property key="TEMPLET_FILE_PATH_WAP">/WEB-INF/def/wap/common/templet.jsp</property>
    <!-- fail文件路徑 -->
    <property key="FAIL_FILE_PATH">/WEB-INF/def/web/common/error/fail.jsp</property>
    <!-- error文件路徑 -->
    <property key="ERROR_FILE_PATH">/WEB-INF/def/web/common/error/error.jsp</property>
     
    <!-- 是否啟用(一級(jí))緩存 -->
    <property key="IS_USE_CACHE">true</property>
     
</properties>

7.創(chuàng)建Controller

package org.anyline.start.home.controller;
 
import org.anyline.entity.DataSet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller("web.home.ArtController")
@RequestMapping("/art")
public class ArtController extends org.anyline.controller.impl.TemplateController {
    protected String dir = "";
    @RequestMapping("l")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView("list.jsp");
        DataSet set = service.query("CMS_ART", config(true,"TITLE:%title%"));
        mv.addObject("set", set);
        return mv;
    }
}

8.創(chuàng)建JSP

jsp中顯示set 

<c:forEach var="item" items="${set}">
${item.TITLE}
</c:forEach> 













以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)