创建ssm项目步骤

2020-05-15 16:05:55来源:博客园 阅读 ()

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

创建ssm项目步骤

步骤

理解web应用流程
1587471344934

1、创建一个Javaweb ssm(maven)项目

参考链接

2、导入依赖

<?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.system</groupId>
  <artifactId>Examination</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Examination Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--servletAPI -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

    <!--shiro依赖 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-core</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-web</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.2.3</version>
    </dependency>

    <!--spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>

    <!--springWEB -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>

    <!--springMVC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>

    <!--spring tx 事务处理 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>

    <!--spring aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>

    <!--spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.5.RELEASE</version>
    </dependency>

    <!--jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!--log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!--mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

    <!--mybatis逆向工程 -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>

    <!--mybatis spring整合包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <!--hibernate 数据校验器包 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.4.1.Final</version>
    </dependency>

    <!--c3p0链接池 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!--Mysql数据库驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.41</version>
    </dependency>
    <dependency>
      <groupId>org.jetbrains</groupId>
      <artifactId>annotations-java5</artifactId>
      <version>RELEASE</version>
    </dependency>

  </dependencies>


  <build>
    <finalName>Examination</finalName>

    <!--在IDEA中maven 默认编译的时候 只搬运src/main/java里面的java文件到target/classes,其他文件会被忽略 -->
    <!--这时,我们Mybatis的Mapper.xml文件就不能被加载进去,也就是不能映射成功 -->
    <!--下面配置就能解决这个问题 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>

    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>xml
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3、配置web.xml

  1. 加载spring容器

  2. 配置springmvc前端控制器

  3. 字符编码过滤器

    <?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_3_1.xsd"
             version="3.1">
    
      <display-name>Archetype Created Web Application</display-name>
    
      <!--加载spring容器-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/applicationContext-*.xml</param-value>
      </context-param>
    
        <!--配置springmvc前端控制器-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
            <!--contextConfigLocation配置SpringMVC加载的配置文件(配置处理器,映射器等等)
        如果不配置contextConfigLocation,默认加载的是:/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
        -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/springmvc.xml</param-value>
            </init-param>
        </servlet>
        <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>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>
    
    

4、资源文件

4.1 db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://locahost:3306/examination
jdbc.username=root
jdbc.password=0000

4.2 applicationContext-dao.xml

  1. 加载外部配置文件
  2. 配置数据源
  3. 配置SqlSessionFactory
  4. Mapper批量扫描
<?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:aop="http://www.springframework.org/schema/aop"
       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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--加载数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--配置数据源 c3p0l连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置SqlSessionFactory-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis/mybatis.cfg.xml"></property>
        <!--加载数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--Mapper批量扫描,从Mapper包扫描接口,自动创建代理对象,并在Spring容器中自动注册
    使用 Mybatis与Spring整合包的这个 Mapper 扫描器后, Mybatis 配置文件里的扫描器,就可以取消掉了
    遵循的规范 不变
    自动扫描出来的Mapper的bean的id为Mapper类名(首字母小写)
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--如果需要扫描多个包下的mapper,每个包中间使用半角逗号分开-->
        <property name="basePackage" value="com.system.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
    </bean>

</beans>

4.3 springmvc.xml

  1. 加载静态资源
  2. 开启注解扫描
  3. 组件扫描
  4. 视图解析器
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--静态资源解析包括 :js、css、img、.. -->
    <!-- <mvc:resources mapping="/js/" location="/js/**" />
    <mvc:resources mapping="/css/" location="/css/**" />
    <mvc:resources mapping="/fonts/" location="/fonts/**" />
    <mvc:resources mapping="/images/" location="/images/**"/>  -->
    <!--加载静态资源 -->
    <mvc:default-servlet-handler />

    <!--<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<开启注解方式:配置>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->

    <!--使用annotation-driven 注解驱动,就可以代替 注解映射器 和 注解适配器 的配置 -->
    <!--conversion-service 向处理器适配器中注入【自定义的参数绑定组件】。 -->
    <!--validator 向处理器适配器,注入 校验器 -->
    <mvc:annotation-driven conversion-service="conversionService">
    </mvc:annotation-driven>

    <!--自定义参数绑定组件 -->
  <!--  <bean id="conversionService"
          class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        &lt;!&ndash; 转换器 &ndash;&gt;
        <property name="converters">
            <set>
                &lt;!&ndash; 日期类型转换 &ndash;&gt;
                <bean class="com.system.controller.converter.CustomDateConverter" />
            </set>
        </property>
    </bean>-->

    <!--组件扫描,可以扫描 controller、Service、... 并注册添加到 spring 容器中 这里扫描 controller,指定controller的包 -->
    <context:component-scan base-package="com.system.controller" />

    <!--全局错误信息处理器 只要实现HandlerExceptionResolver接口就是全局异常处理器 -->
   <!-- <bean class="com.system.exception.CustomExceptionResolver" />-->

    <!--视图解析器 -->
    <!-- 需要配置解析jsp的视图解析器 jsp解析,默认使用jstl标签解析 -->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--拼接视图地址的前缀和后缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

注意事项:

跳转?链接


原文链接:https://www.cnblogs.com/jklixin/p/12894340.html
如有疑问请与原作者联系

标签:app参数HTMLAVServletIE

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring Cloud认知学习(二):Ribbon使用

下一篇:Java GUI编程