zoukankan      html  css  js  c++  java
  • Maven

    一、前述

    Maven 是专门用于构建和管理 Java 相关项目的工具,利用 Maven 的主要目的是统一维护 jar 包。

    SSM(Spring+SpringMVC+MyBatis)框架集由 Spring、MyBatis 两个开源框架整合而成(SpringMVC 是 Spring 中的部分内容)。

    由于 Spring MVC 是 Spring 框架中的一个模块,所以 Spring MVC 与 Spring 之间不存在整合的问题,只要引入相应 JAR 包就可以直接使用。因此 SSM 框架的整合就只涉及到了 Spring 与 MyBatis 的整合,以及 Spring MVC 与 MyBatis 的整合。Spring 与 MyBatis 框架的整合时,通过 Spring 实例化 Bean,然后调用实例对象中的查询方法来执行 MyBatis 映射文件中的 SQL 语句的,如果能够正确查询出数据库中的数据,那么就可以认为 Spring 与 MyBatis 框架整合成功。加入 Spring MVC 后,如果可以通过前台页面来执行查询方法,并且查询出的数据能够在页面中正确显示,那么就可以认为三大框架整合成功。

    二、Maven+SSM框架整合

    首先说明用到的框架: spring  +  springMVC  +  mybatis
    构建工具:maven
    开发工具:eclipse
    开发环境:win10      java版本:jdk1.8      tomcat版本:tomcat8.5

    1)创建Maven项目

    新建Maven项目,选择Maven Project,勾选第一个,然后【Next】下一步。

    设置GroupID(一般为公司名)、ArtifactID(项目名)和Packaging(打包方式)

    【Finish】项目创建完成 - 如果pom文件报错显示web.xml丢失,在src -> main -> webapp下创建WEB-INF目录,并在目录下创建web.xml

    2)利用Maven导入jar包

    前述中我也说了,利用 Maven 的目的就是要统一维护 jar 包,而jar包的维护则是通过一个 pom.xml 文件来实现的,我们可以在目录中看到一个 pom.xml 文件,项目的 jar 包也正是要通过这个文件导入。

    <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.cyan</groupId>
        <artifactId>Maven_SSM</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <!-- 声明版本号 / 统一管理版本 -->
        <properties>
            <!-- lib version - jar版本 -->
            <spring.version>5.0.8.RELEASE</spring.version>
            <mybais.version>3.4.6</mybais.version>
            <mybatis-spring.version>1.3.2</mybatis-spring.version>
            <mysql-connector.version>5.1.46</mysql-connector.version>
            <ojdbc7.version>12.1.0.2.0</ojdbc7.version>
            <druid.version>1.0.16</druid.version>
            <aopalliance.version>1.0</aopalliance.version>
            <aspectj.weaver.version>1.6.4.RELEASE</aspectj.weaver.version>
            <commons.logging.version>1.1.1</commons.logging.version>
            <jstl.version>1.2</jstl.version>
            <log4j.version>1.2.16</log4j.version>
            <slf4j-api.version>1.6.1</slf4j-api.version>
            <slf4j-nop.version>1.6.4</slf4j-nop.version>
            <fileupload.version>1.3.1</fileupload.version>
            <jackson.version>2.9.6</jackson.version>
            <servlet-api.version>8.5.32</servlet-api.version>
            <jsp-api.version>8.5.32</jsp-api.version>
            <!-- plugins version - 插件版本 -->
            <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
            <tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
        </properties>
    
        <!-- 锁定版本 / 避免依赖冲突 -->
        <dependencyManagement>
            <dependencies>
                <!-- spring -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context-support</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-test</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <!-- spring end -->
    
                <!-- mybais -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>${mybais.version}</version>
                </dependency>
                <!-- mybatis - spring 整合包 -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>${mybatis-spring.version}</version>
                </dependency>
    
                <!-- json -->
                <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-annotations</artifactId>
                    <version>${jackson.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                    <version>${jackson.version}</version>
                </dependency>
                <!-- json end -->
            </dependencies>
        </dependencyManagement>
    
        <!-- 配置依赖 - 加入SSM框架所需的依赖 -->
        <dependencies>
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
            </dependency>
            <!-- spring end -->
    
            <!-- mybais核心包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </dependency>
    
            <!-- mybatis - spring 整合包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </dependency>
    
            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
    
            <dependency>
                <groupId>cn.easyproject</groupId>
                <artifactId>ojdbc7</artifactId>
                <version>${ojdbc7.version}</version>
            </dependency>
            <!-- mysql end -->
    
            <!-- 阿里巴巴的数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
    
            <!-- aop -->
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>${aopalliance.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>com.springsource.org.aspectj.weaver</artifactId>
                <version>${aspectj.weaver.version}</version>
            </dependency>
            <!-- aop end -->
    
            <!-- logging -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>com.springsource.org.apache.commons.logging</artifactId>
                <version>${commons.logging.version}</version>
            </dependency>
    
            <!-- jsp标准标签库 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
    
            <!-- log - 日志文件管理包 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j-api.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>${slf4j-nop.version}</version>
                <!-- 避免jar冲突 - 排除依赖导入 -->
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- log end -->
    
            <!--文件上传组件包 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${fileupload.version}</version>
            </dependency>
    
            <!-- json -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </dependency>
            <!-- json end -->
    
            <!-- servlet-api 8.5.32 -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- jsp-api 8.5.32 -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <!-- 配置插件 -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>${tomcat7-maven-plugin.version}</version>
                    <configuration>
                        <url>http://localhost:8080/manager/text</url>
                        <username>alin</username>
                        <password>123123</password>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

    3)pom.xml配置Maven Resources资源放行

    ① 因为 Maven 会把 src/main/java 下的 *.java 文件文件编译放到 target/classes 目录目录中,但这目录中的其他文件会被忽略;
    ② Mybatis Mapper 映射文件也要放在 src/main/java 下,所以不能忽略;
    ③ 因此我们需要对 src/main/java 下的配置文件进行过滤,不能被忽略;
    ④ 利用 Maven 中的 Resources 标签可以进行配置;
    <!-- 配置Maven Resources资源放行 -->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <!-- 放行main/resources下的配置文件 如果不放行spring mybatis等配置文件不会被打到包里从而导致错误 -->
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>

    4)创建SSM框架所需配置文件

    在 src -> main -> resources 目录下创建

    建立 jdbc 属性配置文件 - db.properties

    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_springmvc?useUnicode=true&characterEncoding=utf-8&useSSL=true
    jdbc.user=root
    jdbc.password=123456

    分层建立 Spring 配置文件

     - Dao层:读取 db.properties 文件的配置和数据源配置,配置Mybatis与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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
        <!-- 读取配置文件:数据库 -->
        <context:property-placeholder location="classpath:db.properties" />
    
        <!-- 配置C3P0数据源 -->
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driverClass}" />
            <property name="url" value="${jdbc.jdbcUrl}" />
            <property name="username" value="${jdbc.user}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
        <!-- 配置Mybatis -->
        <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!-- 配置bean别名 -->
            <property name="typeAliasesPackage" value="com.cyan.bean" />
        </bean>
    
        <!-- 配置mapper工厂 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cyan.mapper" />
        </bean>
    
    </beans>
    applicationContext_mapper.xml

     - Service层:开启事物、Service注解,配置事务管理器

    <?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-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="cn.alin.service" />
    
    </beans>
    applicationContext_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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
        <!-- 事务核心管理器 -->
        <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 注解事务 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    </beans>
    applicationContext_transaction.xml

     - MVC层:配置用于扫描 @Controller 注解的包扫描器、注解驱动器以及视图解析器

    <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
        <!-- 使用配置文件解决查询词典时硬编码问题 -->
        <context:property-placeholder location="classpath:dictTypeid.properties" />
    
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="cn.alin.controller" />
    
        <!-- 开启注解驱动 -->
        <mvc:annotation-driven />
    
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
        <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 上传文件的编码 -->
            <property name="defaultEncoding" value="UTF-8" />
        </bean>
    
    </beans>
    springmvc.xml

    5)修改 web.xml 文件

    配置 spring 的文件监听器、编码过滤器以及 Spring MVC 的前端控制器等信息。

    <?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_3_1.xsd" id="WebApp_ID" version="3.1">
        <display-name>maven_ssm</display-name>
    
        <!-- 静态资源过滤 -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.css</url-pattern>
            <url-pattern>*.ttf</url-pattern>
            <url-pattern>*.woff</url-pattern>
            <url-pattern>*.js</url-pattern>
            <url-pattern>*.png</url-pattern>
            <url-pattern>*.jpg</url-pattern>
            <url-pattern>*.gif</url-pattern>
        </servlet-mapping>
    
        <!-- 启动Spring -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext_*.xml</param-value>
        </context-param>
    
        <!-- 配置前端控制器 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置springmvc加载的配置文件(配置处理器映射器、适配器等) -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <!-- 过滤器 解决POST提交中文乱码问题 -->
        <filter>
            <filter-name>encoding</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>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
    </web-app>

    6)测试

    到这里利用 Maven 整合 SSM 框架的所有操作已经完成了,那么接下来让我们验证一下,刚刚的整合操作是否存在问题呢?

    建立 bean 持久化对象 - 用于映射数据库表的持久化类。

    package com.cyan.bean;
    
    public class User {
    
        private String username;
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "User [username=" + username + ", password=" + password + "]";
        }
    
    }
    User.java
    package com.cyan.bean;
    
    public class ItemInfo {
    
        // id
        private String item_id;
        // name
        private String item_name;
        // type
        private String item_type;
        // price
        private Double item_price;
    
        public String getItem_id() {
            return item_id;
        }
    
        public void setItem_id(String item_id) {
            this.item_id = item_id;
        }
    
        public String getItem_name() {
            return item_name;
        }
    
        public void setItem_name(String item_name) {
            this.item_name = item_name;
        }
    
        public String getItem_type() {
            return item_type;
        }
    
        public void setItem_type(String item_type) {
            this.item_type = item_type;
        }
    
        public Double getItem_price() {
            return item_price;
        }
    
        public void setItem_price(Double item_price) {
            this.item_price = item_price;
        }
    
        @Override
        public String toString() {
            return "ItemInfo [item_id=" + item_id + ", item_name=" + item_name + ", item_type=" + item_type
                    + ", item_price=" + item_price + "]";
        }
    
    }
    ItemInfo.java

    ② 建立 dao层 mapper 接口文件和映射文件

    package com.cyan.mapper;
    
    import java.util.List;
    
    import com.cyan.bean.ItemInfo;
    
    public interface ItemMapper {
    
        // 获取游戏列表
        List<ItemInfo> getAllList();
    }
    ItemMapper.java
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.cyan.mapper.ItemMapper">
    
        <!-- List<ItemInfo> getAllList(); -->
        <select id="getAllList" resultType="ItemInfo">
            SELECT item_id,item_name,item_type,item_price
            FROM item_info
        </select>
    
    </mapper>
    ItemMapper.xml

    ③ 建立 service 接口与 service 实现类

    实现类采用了基于注解的形式, @Service 注解标识业务层的实现类, @Autowired 注解将 ItemMapper 接口对象注入到类中。

    package com.cyan.service;
    
    import java.util.List;
    
    import com.cyan.bean.ItemInfo;
    
    public interface ItemService {
        
        List<ItemInfo> getAllList();
    }
    ItemService.java
    package com.cyan.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.cyan.bean.ItemInfo;
    import com.cyan.mapper.ItemMapper;
    
    @Service
    public class ItemServiceImpl implements ItemService {
    
        @Autowired
        private ItemMapper itemMapper;
    
        @Override
        public List<ItemInfo> getAllList() {
            return itemMapper.getAllList();
        }
    
    }
    ItemServiceImpl.java

    ④ 建立 controller 控制层对象

    package com.cyan.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.cyan.bean.ItemInfo;
    import com.cyan.bean.User;
    import com.cyan.service.ItemService;
    
    @Controller
    public class UserConteoller {
    
        @Autowired
        private ItemService itemService;
    
        // 后台接收用户名和密码,跳转页面,并显示
        @RequestMapping("/login")
        public String login(User user, Model model) {
            // 打印到服务器控制台
            System.err.println("user = " + user);
            // 传递到jsp页面
            model.addAttribute("User", user);
            return "list";
        }
    
        // 读取数据库 显示游戏列表
        @RequestMapping("/list")
        public String list(Model model) {
    
            List<ItemInfo> itemList = itemService.getAllList();
    
            // 打印到服务器控制台
            for (ItemInfo itemInfo : itemList) {
                System.out.println("itemInfo = " + itemInfo);
            }
    
            model.addAttribute("itemList", itemList);
            return "item_list";
        }
    
    }
    UserConteoller.java

    ⑤ 建立 jsp 文件

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Login...</title>
    </head>
    <body>
    
        <!-- 输入用户名密码 提交表单到后台,跳转页面显示用户名密码 -->
        <form action="${pageContext.request.contextPath }/login" method="get">
            <input type="text" name="username" /> 
            <input type="text" name="password" /> 
            <input type="submit" value="确认" />
        </form>
    
        <!-- 获取数据库游戏列表信息 -->
        <button onclick="window.location='${pageContext.request.contextPath }/list'">游戏列表</button>
    
    </body>
    </html>
    login.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
        <br /> 用户名:${User.username }
        <br /> 密码:${User.password }
    
    </body>
    </html>
    list.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
        <c:forEach items="${itemList }" var="item">
            <label>${item.item_name } </label>
            <label>${item.item_type } </label>
            <br />
        </c:forEach>
    
    </body>
    </html>
    item_list.jsp

    ⑥ 将项目发布到 tomcat 中右键项目 -> Run As -> Maven build...  -> tomcat7:deploy 或 tomcat7:redeploy

    输入URL测试 - 表单输入Alin / 666666

    确认键:

    游戏列表按钮:

    Tomcat输出:

  • 相关阅读:
    01点睛Spring MVC 4.1-搭建环境
    18点睛Spring4.1-Meta Annotation
    17点睛Spring4.1-@Conditional
    16点睛Spring4.1-TaskScheduler
    15点睛Spring4.1-TaskExecutor
    Zabbix4.0.3解决中文乱码
    A10映射方法
    源码安装zabbix_agent4.0.3
    单机部署redis5.0集群环境
    zabbix系列之九——添加钉钉告警
  • 原文地址:https://www.cnblogs.com/Dm920/p/12394840.html
Copyright © 2011-2022 走看看