Dubbo 03: 直连式 + 接口工程( 三 )

  • 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: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.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"><!-- 扫描组件 --><context:component-scan base-package="com.example.dubbo.web.controller"/><!-- 注解驱动 --><mvc:annotation-driven/><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="https://www.huyubaike.com/"/><property name="suffix" value="https://www.huyubaike.com/biancheng/.jsp"/></bean></beans>
  • Controller层
    package com.example.dubbo.web.controller;import com.example.dubbo.model.User;import com.example.dubbo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class UserController {@AutowiredUserService userService;/*** 响应前端请求,返回用用户详细信息以及总的用户个数*/@RequestMapping("/getUserDetail.do")public String getUserDetail(String id, Model model){//获取数据User user = userService.queryUserById(id);int userCount = userService.queryAllUserCount();//存放数据model.addAttribute("user", user);model.addAttribute("userCount", userCount);//跳转到用户详情页面return "userDetail";}}
  • web.xml
    <?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_4_0.xsd"version="4.0"><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:application.xml classpath:dubbo-link-consumer.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
  • 返回给前端的响应页面:userDetail.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>用户详情页</title></head><body><div>用户id:${user.id}</div><div>用户名:${user.name}</div><div>用户年龄:${user.age}</div><div>用户数量:${userCount}</div></body></html>
  • 测试
    • 将服务提供者工程和消费者工程部署到tomcat上并运行
      Dubbo 03: 直连式 + 接口工程

      文章插图
    • 运行结果
      Dubbo 03: 直连式 + 接口工程

      文章插图
    分析