天天看點

Day43項目saas-export項目-企業入駐申請-前台**

消費者Controller

  • (1)分析

    服務提供者已經有了:前端系統就是服務的消費者,調用服務

    建立一個工程,編寫Controller調用Service

  • (2)實作步驟

    》1 建立項目:export_web_portal

    》2 添加依賴: 依賴企業管理service接口工程、依賴dubbo

pom.xml

<parent>
    <groupId>com.wzx</groupId>
    <artifactId>export_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>com.wzx</groupId>
  <artifactId>export_web_portal</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>export_web_portal</name>

  <dependencies>
    <!-- 依賴接口-->
    <dependency>
      <groupId>com.wzx</groupId>
      <artifactId>export_company_interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--dubbo支援包-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.6</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.32.Final</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.0.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.7</version>
    </dependency>
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>


  </dependencies>      

》3 配置web.xml

web.xml

完成編碼過濾器,前端控制器,還要作為一個main方法加載配置檔案

<!-- main-->
  <!-- 解決post亂碼 -->
  <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>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 指定加載的配置檔案 ,通過參數contextConfigLocation加載-->
    <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>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.do</welcome-file>
  </welcome-file-list>      

》4 配置springmvc.xml

springmvc.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:mvc="http://www.springframework.org/schema/mvc" 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">

    <!--1.springmvc配置-->
    <!--1.1 controller掃描-->
    <context:component-scan base-package="com.wzx.web"></context:component-scan>

    <!--1.2 視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--1.3 mvc注解驅動-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--2.dubbo消費方配置-->
    <!--2.1 服務名稱-->
    <dubbo:application name="export_web_portal"></dubbo:application>

    <!--2.2 Zookeeper連接配接配置-->
    <dubbo:registry address="zookeeper://localhost" port="2181"></dubbo:registry>

    <!--2.3 掃描@Reference注解-->
    <dubbo:annotation package="com.wzx.web"></dubbo:annotation>

</beans>      

消費者Controller測試

》5 部署UI資源

複制資料裡面準備好的檔案

Day43項目saas-export項目-企業入駐申請-前台**

每個頁面要設定path 作為項目位址

》6 編寫控制器 ApplyController

ApplyController

@Controller
public class ApplyController {
    private Logger l = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(path="index.do" ,method = {RequestMethod.GET,RequestMethod.POST})
    public String index(){
        return "index";
    }
    @RequestMapping(path="commit.do" ,method = {RequestMethod.GET,RequestMethod.POST})
    public String commit(){
        return "commit";
    }
    @RequestMapping(path="toApply.do" ,method = {RequestMethod.GET,RequestMethod.POST})
    public String toApply(){
        return "apply";
    }

    // Serialized class com.wzx.domain.company.Company must implement java.io.Serializable
    //因為rpc底層将對象序列化成二進制資料寫到另一個伺服器上。
    //在java需要對類實作序列化接口 Serializable
    @Reference //通過rpc查找 provider中的實作類對象
    ICompanyService iCompanyService;
    @RequestMapping(path="apply.do" ,method = {RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody
    String apply(Company company){
        //調用service進行儲存
        l.info("apply company "+company);

        try {
            iCompanyService.saveCompany(company);//表示成功
            return "1";
        } catch (Exception e) {//表示失敗
            e.printStackTrace();
            return "-1";
        }

    }
}