美文网首页
07-dubbo练习

07-dubbo练习

作者: bin_lifecycle | 来源:发表于2019-11-29 10:54 被阅读0次

    1.新建聚合项目 dubboparent2

    新建父项目,选择maven项目,选择pom打包方式,删除src目录,只保留pom.xml文件,项目目录如下:

    dubboparent2项目目录

    pom.xml

    <?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.sccl</groupId>
        <artifactId>dubbo-parent2</artifactId>
        <packaging>pom</packaging>
        <version>0.0.1-SNAPSHOT</version>
    
        <modules>
            <module>menu-pojo</module>
            <module>menu-service</module>
            <module>menu-dubbo</module>
            <module>menu-management</module>
        </modules>
    
        <properties>
            <servlet-version>3.0.1</servlet-version>
            <jsp-version>2.2</jsp-version>
            <jstl-version>1.2</jstl-version>
            <spring-version>4.1.6.RELEASE</spring-version>
            <aspectjweaver-version>1.8.6</aspectjweaver-version>
            <mybatis-version>3.2.7</mybatis-version>
            <mybatis-spring-version>1.2.3</mybatis-spring-version>
            <log4j-version>1.2.17</log4j-version>
            <mysql-connector-java-version>5.1.38</mysql-connector-java-version>
            <jackson-version>2.4.1</jackson-version>
            <commons-fileupload-version>1.3.1</commons-fileupload-version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>${servlet-version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>javax.servlet.jsp</groupId>
                    <artifactId>jsp-api</artifactId>
                    <version>${jsp-version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>jstl</groupId>
                    <artifactId>jstl</artifactId>
                    <version>${jstl-version}</version>
                </dependency>
                <!-- spring -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectjweaver-version}</version>
                </dependency>
                <!-- mybatis -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>${mybatis-version}</version>
                </dependency>
                <!-- mybatis和spring整合 -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>${mybatis-spring-version}</version>
                </dependency>
                <!-- log4j 日志 -->
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j-version}</version>
                </dependency>
                <!-- mysql 驱动类 -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql-connector-java-version}</version>
                </dependency>
                <!-- jackson -->
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                    <version>${jackson-version}</version>
                </dependency>
                <!-- apache 图片上传 -->
                <dependency>
                    <groupId>commons-fileupload</groupId>
                    <artifactId>commons-fileupload</artifactId>
                    <version>${commons-fileupload-version}</version>
                </dependency>
                <!-- dubbo -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>dubbo</artifactId>
                    <version>2.5.3</version>
                    <exclusions>
                        <exclusion>
                            <artifactId>spring</artifactId>
                            <groupId>org.springframework</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <!-- 访问zookeeper的客户端jar -->
                <dependency>
                    <groupId>com.101tec</groupId>
                    <artifactId>zkclient</artifactId>
                    <version>0.10</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                </resource>
            </resources>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <!-- 控制tomcat端口号 -->
                            <port>80</port>
                            <!-- 项目发布到tomcat后的名称 -->
                            <!-- / 相当于把项目发布名称为ROOT -->
                            <!-- /abc -->
                            <path>/</path>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    
    </project>
    

    2.新建子模块 menu-pojo,继承父项目dubbo-parent2

    menu-pojo的项目目录:

    menu-pojo项目目录

    menu-pojo下的实体类:

    Menu

    package com.sccl.pojo;
    
    import java.io.Serializable;
    import java.util.List;
    
    /** 菜单实体类
     * Create by wangbin
     * 2019-11-28-10:19
     */
    public class Menu implements Serializable {
        private Integer id;
        private String name;
        private Integer pid;
        private List<Menu> children;
        private Menu parent;
    
        public Menu getParent() {
            return parent;
        }
    
        public void setParent(Menu parent) {
            this.parent = parent;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getPid() {
            return pid;
        }
    
        public void setPid(Integer pid) {
            this.pid = pid;
        }
    
        public List<Menu> getChildren() {
            return children;
        }
    
        public void setChildren(List<Menu> children) {
            this.children = children;
        }
    }
    

    menu-pojo的pom.xml文件:实体模块继承父项目

    <?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">
        <parent>
            <artifactId>dubbo-parent2</artifactId>
            <groupId>com.sccl</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>menu-pojo</artifactId>
    
    </project>
    

    2.新建子模块 menu-service,继承父项目dubbo-parent2

    menu-service的项目目录:

    menu-service的项目目录

    menu-service下定义的接口:

    MenuDubboService

    package com.sccl.dubbo.service;
    
    import com.sccl.pojo.Menu;
    
    import java.util.List;
    
    /**
     * Create by wangbin
     * 2019-11-28-10:44
     */
    public interface MenuDubboService {
        List<Menu> findAll();
        Integer updateMenu(Menu menu);
        Menu selectById(Integer id);
    }
    

    menu-service的pom.xml文件,dubbo的接口模块继承父项目,依赖了menu-pojo实体模块

    <?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">
        <parent>
            <artifactId>dubbo-parent2</artifactId>
            <groupId>com.sccl</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>menu-service</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.sccl</groupId>
                <artifactId>menu-pojo</artifactId>
                <version>0.0.1-SNAPSHOT</version>
    </dependency>
        </dependencies>
    
    </project>
    

    3.新建子模块 menu-dubbo,继承父项目dubbo-parent2

    menu-dubbo的项目目录:


    menu-dubbo的项目目录

    menu-duubo (服务提供者模块)的实现类,实现menu-service接口模块中定义的借接口:

    MenuDubboServiceImpl

    package com.sccl.dubbo.service.impl;
    
    import com.sccl.dubbo.service.MenuDubboService;
    import com.sccl.mapper.MenuMapper;
    import com.sccl.pojo.Menu;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    /**
     * Create by wangbin
     * 2019-11-28-12:07
     */
    public class MenuDubboServiceImpl implements MenuDubboService {
        @Resource
        private MenuMapper menuMapper;
        @Override
        public List<Menu> findAll() {
            List<Menu> list = menuMapper.findAll();
            for (Menu menu: list) {
                if(menu.getPid() != 0){
                    menu.setParent(menuMapper.selectById(menu.getPid()));
                }
                menu.setChildren(menuMapper.selectByPid(menu.getId()));
            }
            return list;
        }
    
        @Override
        public Integer updateMenu(Menu menu) {
            return menuMapper.updateMenu(menu);
        }
    
        @Override
        public Menu selectById(Integer id) {
            return menuMapper.selectById(id);
        }
    }
    
    

    MenuMapper

    package com.sccl.mapper;
    import com.sccl.pojo.Menu;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    import java.util.List;
    
    /**
     * Create by wangbin
     * 2019-11-28-11:37
     */
    public interface MenuMapper {
        @Select("select * from menu")
        List<Menu> findAll();
        @Select("select * from menu where id = #{id}")
        Menu selectById(Integer id);
        @Select("select * from menu where pid = #{pid}")
        List<Menu> selectByPid(Integer pid);
        @Update("update menu set pid=#{pid},name=#{name} where id = #{id}")
        Integer updateMenu(Menu menu);
    }
    

    Test 用于启动服务

    package com.sccl.test;
    
    
    import com.alibaba.dubbo.container.Main;
    
    /**
     * Create by wangbin
     * 2019-11-28-12:19
     */
    public class Test {
        public static void main(String[] args) {
            Main.main(args);
        }
    }
    

    resources目录:


    resources目录

    applicationContext-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
        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://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!--应用名称-->
        <dubbo:application name="dubbo-service"/>
        <!--注册中心-->
        <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"></dubbo:registry>
        <!--使用的协议-->
        <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
        <!--暴露的接口-->
        <dubbo:service interface="com.sccl.dubbo.service.MenuDubboService" ref="menuDubboServiceImpl"></dubbo:service>
        <!--实现类-->
        <bean id="menuDubboServiceImpl" class="com.sccl.dubbo.service.impl.MenuDubboServiceImpl"></bean>
    
        <!-- 加载Dubbo容器,同时把spring配置文件也加载 -->
        <import resource="../../applicationContext.xml"/>
    </beans>
    

    applicationContext.xml spring配置文件

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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/aop
            http://www.springframework.org/schema/aop/spring-aop.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" default-autowire="byName">
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.sccl.dubbo.service.impl"></context:component-scan>
        <!-- 加载属性文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
        <!-- SqlSessionFactory -->
        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="typeAliasesPackage" value="com.sccl.pojo"></property>
        </bean>
        <!-- 扫描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.sccl.mapper"></property>
            <property name="sqlSessionFactoryBeanName" value="factory"></property>
        </bean>
        <!-- 事务管理器 -->
        <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 声明式事务 -->
        <tx:advice id="txAdvice" transaction-manager="txManage">
            <tx:attributes>
                <tx:method name="ins*"/>
                <tx:method name="del*"/>
                <tx:method name="upd*"/>
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        <!-- 配置aop -->
        <aop:config>
            <aop:pointcut expression="execution(* com.sccl.dubbo.service.impl.*.*(..))" id="mypoint"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
        </aop:config>
    </beans>
    

    db.properties 数据库配置文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1:3306/ssm
    jdbc.username = root
    jdbc.password = 123456
    

    log4j.properties 日志文件配置

    ···
    log4j.rootCategory=ERROR, CONSOLE ,LOGFILE

    log4j.logger.com.bjsxt.mapper=DEBUG

    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss} %m %n

    log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    log4j.appender.LOGFILE.File=E:/my.log
    log4j.appender.LOGFILE.Append=true
    log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.LOGFILE.layout.ConversionPattern=%m %n
    ···

    4.新建子模块 menu-management,继承父项目dubbo-parent2

    menu-management项目目录


    menu-management项目目录

    MenuService (menu-management 定义的接口)

    package com.sccl.service;
    
    import com.sccl.pojo.Menu;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * Create by wangbin
     * 2019-11-28-13:34
     */
    public interface MenuService {
        List<Menu> show();
        Map<String,Object> showMenu(Integer id);
        Integer updateMenu(Menu menu);
    }
    
    

    MenuServiceImpl 调用dubbo服务接口,接口调用代理进行具体实现

    package com.sccl.service.impl;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.sccl.dubbo.service.MenuDubboService;
    import com.sccl.pojo.Menu;
    import com.sccl.service.MenuService;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /** web项目中实现业务时调用dubbo的接口不再直接调用mapper层,
     * dubbo接口自己去找具体的实现来实现业务
     * Create by wangbin
     * 2019-11-28-13:39
     */
    @Service
    public class MenuServiceImpl implements MenuService {
        //注入dubbo服务的接口,并调用
        @Reference
        private MenuDubboService menuDubboService;
        @Override
        public List<Menu> show() {
            return menuDubboService.findAll();
        }
    
        @Override
        public Map<String,Object> showMenu(Integer id) {
            Map<String,Object> map = new HashMap<>();
            map.put("menu",menuDubboService.selectById(id));
            map.put("allMenu",menuDubboService.findAll());
            return map;
        }
    
        @Override
        public Integer updateMenu(Menu menu) {
            return menuDubboService.updateMenu(menu);
        }
    }
    

    MenuContrller (正常调用service)

    package com.sccl.controller;
    
    import com.sccl.pojo.Menu;
    import com.sccl.service.MenuService;
    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 javax.annotation.Resource;
    import java.util.Map;
    
    /**
     * Create by wangbin
     * 2019-11-28-13:46
     */
    @Controller
    public class MenuContrller {
        @Resource
        private MenuService menuService;
        @RequestMapping("/show")
        public String show(Model model){
            model.addAttribute("list",menuService.show());
            return "/index.jsp";
        }
        @RequestMapping("/showMenu")
        public String showMenu(Integer id,Model model){
            Map<String,Object> map = menuService.showMenu(id);
            model.addAttribute("menu",map.get("menu"));
            model.addAttribute("allMenu",map.get("allMenu"));
            return "/update.jsp";
        }
        @RequestMapping("update")
        public String update(Menu menu){
            int index = menuService.updateMenu(menu);
            if(index>0){
                return "/show";
            }else{
                return "redirect:/showMenu?id="+menu.getId();
            }
        }
    }
    

    applicationContext-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
        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://code.alibabatech.com/schema/dubbo 
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!-- 给当前Provider自定义个名字 -->
        <dubbo:application name="dubbo-consumer"/>
        <!-- 配置注册中心  -->
        <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"></dubbo:registry>
        <!-- 配置注解扫描 -->
        <dubbo:annotation package="com.sccl.service.impl"/>
    </beans>
    

    application-spring.xml (spring配置)

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 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/aop
            http://www.springframework.org/schema/aop/spring-aop.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"
        default-autowire="byName">
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.sccl.service.impl"></context:component-scan>
    </beans>
    

    springmvc.xml (springmvc配置文件)

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
           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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <!-- 注解驱动 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 扫描@Controller注解 -->
        <context:component-scan base-package="com.sccl.controller"></context:component-scan>
        <!-- 静态资源 -->
        <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
        <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
        <mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
    </beans>
    

    db.properties (数据库配置)

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1/ssm
    jdbc.username = root
    jdbc.password = 123456
    

    log4j.properties (日志配置)

    ···
    log4j.rootCategory=ERROR, CONSOLE ,LOGFILE

    log4j.logger.com.bjsxt.mapper=DEBUG

    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss} %m %n

    log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    log4j.appender.LOGFILE.File=E:/my.log
    log4j.appender.LOGFILE.Append=true
    log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.LOGFILE.layout.ConversionPattern=%m %n
    ···

    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">
        <!-- 上下文参数 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-*.xml</param-value>
        </context-param>
        <!-- 监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- SpringMVC前端控制器 -->
        <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:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- 字符编码过滤器 -->
        <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>
    

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <table border="1">
            <thead>
                <tr>
                    <td>编号</td>
                    <td>名称</td>
                    <td>父菜单名称</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="menu" items="${list}">
                    <tr>
                        <td>
                            ${menu.id}
                        </td>
                        <td>
                            ${menu.name}
                        </td>
                        <td>
                            <c:if test="${menu.parent==null}">
                                无
                            </c:if>
                            <c:if test="${menu.parent!=null}">
                                ${menu.parent.name}
                            </c:if>
                        </td>
                        <td>
                            <a href="showMenu?id=${menu.id}">修改</a>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </body>
    </html>
    
    

    update.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="update" method="post">
    <input type="hidden" name="id" value="${menu.id }"/>
    父菜单:<select name="pid">
            <option value="0">根菜单</option>
            <c:forEach items="${allMenu }" var="am">
                <option value="${am.id }" <c:if test="${am.id == menu.pid }">selected="selected"</c:if>   >${am.name }</option>
            </c:forEach>
        </select><br/>
    当前菜单名称:<input type="text" name="name" value="${menu.name }"/><br/>
    <input type="submit" value="修改"/>
    </form>
    </body>
    </html>
    
    

    数据库数据

    数据库数据

    5.启动项目,并测试

    5.1 启动注册zookeeper中心
    为了方便测试,直接用的windows上启动的zookeeper
    5.2 启动服务提供者,启动menu-dubbod的Test类,加载dubbo容器,同时引入spring的配置文件
    服务启动
    5.3 配置消费者方,启动消费者
    点击配置,给web项目配置tomcat 选本地tomcat 选择web项目的war包

    启动web项目:

    web项目启动

    启动后的前台显示的效果:http://localhost:8082/web/

    前台页面

    访问:http://localhost:8082/web/show

    显示所有菜单

    点击修改菜单设置http://localhost:8082/web/showMenu?id=2

    菜单设置-1 菜单设置-2 修改父菜单为根菜单 菜单设置修改后结果

    相关文章

      网友评论

          本文标题:07-dubbo练习

          本文链接:https://www.haomeiwen.com/subject/hzewwctx.html