首页 > Java开发 > Struts2+Spring+jsp调用webservice接口

Struts2+Spring+jsp调用webservice接口

1、书写Action类名为:Struts2ClientAction.java。源码如下:(返回复杂结果集转成json。)

[html][/html] view plaincopy

  1. package com.itcast.struts2.client;
  2. import com.itcast.cxf.first.IHelloWorld;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. /**
  5.  * Struts2访问WebService
  6.  * @author wangjianme
  7.  */
  8. public class Struts2ClientAction extends ActionSupport{
  9.     private static final long serialVersionUID = 1L;
  10.     private IHelloWorld hello;      //注入属性
  11.     private String name;            //用户输入的姓名
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.     public IHelloWorld getHello() {
  19.         return hello;
  20.     }
  21.     public void setHello(IHelloWorld hello) {
  22.         this.hello = hello;
  23.     }
  24.     public String execute() throws Exception {
  25.         name = getHello().sayHello(getName());          //远程调用
  26.         return SUCCESS;
  27.     }
  28. }

2、applicationContext.xml的最终源代码如下

[html][/html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:jaxws="http://cxf.apache.org/jaxws"
  5.        xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  6.        xmlns:cxf="http://cxf.apache.org/core"
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  9.             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
  10.             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
  11.             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
  12.     <jaxws:client address="http://localhost:9999/cxf2.4_spring_web/ws/helloworld"
  13.                   serviceClass="com.itcast.cxf.first.IHelloWorld"
  14.                   id="helloService">
  15.     </jaxws:client>
  16.     <!-- 配置Action -->
  17.     <bean id="Struts2ClientAction"
  18.           class="com.itcast.struts2.client.Struts2ClientAction"
  19.           scope="prototype">
  20.         <property name="hello" ref="helloService"></property>
  21.     </bean>
  22. </beans>

3、配置struts.xml文件的源代码如下:

[html][/html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6.     <package name="com.itcast" extends="struts-default" namespace="/">
  7.         <action name="struts2client" class="Struts2ClientAction">
  8.             <result>/jsps/result.jsp</result>
  9.         </action>
  10.     </package>
  11. </struts>

4、jsps/struts2.jsp的源代码如下:

[html][/html] view plaincopy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <html>
  4.   <head>
  5.   </head>
  6.   <body>
  7.     <form action="<c:url value='/struts2client.action'/>" method="post">
  8.     <label for="name">输入姓名:</label>
  9.     <input type="text" name="name" id="name"/>
  10.     <br/>
  11.     <input type="submit" value="确定"/>
  12.     </form>
  13.   </body>
  14. </html>

5、jsps/result.jsp页面如下

[html][/html] view plaincopy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  2. <%@ taglib uri="/struts-tags" prefix="s"%>
  3. <html>
  4.   <head>
  5.   </head>
  6.   <body>
  7.         <s:property value="name"/>
  8.   </body>
  9. </html>

本文固定链接: http://www.devba.com/index.php/archives/2086.html | 开发吧

报歉!评论已关闭.