当前位置:首页 > 企业简介 >

四、Struts2+Spring+jsp调用webservice接口

作者:曲靖市宁江木业有限公司 来源:www.ynnjmy.com 发布时间:2017-09-10 09:39:16

四、Struts2+Spring+jsp调用webservice接口

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

package com.itcast.struts2.client; import com.itcast.cxf.first.IHelloWorld; import com.opensymphony.xwork2.ActionSupport; /** * Struts2访问WebService * @author wangjianme */ public class Struts2ClientAction extends ActionSupport{ private static final long serialVersionUID = 1L; private IHelloWorld hello; //注入属性 private String name; //用户输入的姓名 public String getName() { return name; } public void setName(String name) { this.name = name; } public IHelloWorld getHello() { return hello; } public void setHello(IHelloWorld hello) { this.hello = hello; } public String execute() throws Exception { name = getHello().sayHello(getName()); //远程调用 return SUCCESS; } } package com.itcast.struts2.client; import com.itcast.cxf.first.IHelloWorld; import com.opensymphony.xwork2.ActionSupport; /** * Struts2访问WebService * @author wangjianme */ public class Struts2ClientAction extends ActionSupport{ private static final long serialVersionUID = 1L; private IHelloWorld hello; //注入属性 private String name; //用户输入的姓名 public String getName() { return name; } public void setName(String name) { this.name = name; } public IHelloWorld getHello() { return hello; } public void setHello(IHelloWorld hello) { this.hello = hello; } public String execute() throws Exception { name = getHello().sayHello(getName()); //远程调用 return SUCCESS; }

2、applicationContext.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:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:client address="http://localhost:9999/cxf2.4_spring_web/ws/helloworld" serviceClass="com.itcast.cxf.first.IHelloWorld" id="helloService"> </jaxws:client> <!-- 配置Action --> <bean id="Struts2ClientAction" class="com.itcast.struts2.client.Struts2ClientAction" scope="prototype"> <property name="hello" ref="helloService"></property> </bean> </beans> <?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:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:client address="http://localhost:9999/cxf2.4_spring_web/ws/helloworld" serviceClass="com.itcast.cxf.first.IHelloWorld" id="helloService"> </jaxws:client> <!-- 配置Action --> <bean id="Struts2ClientAction" class="com.itcast.struts2.client.Struts2ClientAction" scope="prototype"> <property name="hello" ref="helloService"></property> </bean> </beans>

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

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

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

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:鄂州网站制作 http://ezhou.666rj.com

  • 上一篇:Django教程:第一个Django应用程序(1部分)
  • 下一篇:最后一页