从0开始创建环境(spring mvc)
2022-09-16 09:37 更新
1.创建web项目
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内容
<?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" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<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" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 对于长时间不使用的连接强制关闭 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="120" />
<!-- 配置监控统计拦截的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&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和与之对应的数据源 -->
<entry key="ds_def" value-ref="ds_def"></entry>
</map>
</property>
<!-- 这里可以指定默认的数据源 -->
<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配置文件,包含了一些常用的配置参数
<?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>
<!-- 多长时间加载一次配置文件 (秒)-->
<property key="RELOAD">120</property>
<!-- 存放在servlet context中的key(配置文件热重载时不覆盖当前key)(如el表达式中引用${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>
<!-- 国际化文本存放目录 -->
<property key="I18N_MESSAGE_DIR">/WEB-INF/classes/message</property>
<!-- 国际化消息SESSION存放KEY -->
<property key="I18N_MESSAGE_SESSION_KEY">I18N_MESSAGE_SESSION_KEY</property>
<!-- 国际化消息默认语言 -->
<property key="I18N_MESSAGE_DEFAULT_LANG">cn</property>
<!-- 默认主键 -->
<property key="DEFAULT_PRIMARY_KEY">ID</property>
<!-- 自动添加主键 -->
<property key="AUTO_CREATE_PRIMARY_KEY">true</property>
<!-- 是否显示执行的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>
<!-- 登录验证码图片宽度 -->
<property key="VALIDATE_CODE_IMG_WIDTH">60</property>
<!-- 登录验证码图片宽度 -->
<property key="VALIDATE_CODE_IMG_HEIGHT">20</property>
<!-- 登录验证码图片 字符个数 -->
<property key="VALIDATE_CODE_IMG_CHAR_SIZE">4</property>
<!-- 登录验证码图片 复杂度 -->
<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>
<!-- 生成主键的存储过程 按3个输入参数的格式:表,列,其他-->
<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>
<!-- 是否启用(一级)缓存 -->
<property key="IS_USE_CACHE">true</property>
</properties>
7.创建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.创建JSP
jsp中显示set
<c:forEach var="item" items="${set}">
${item.TITLE}
</c:forEach>
以上内容是否对您有帮助:
更多建议: