四、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的源代码如下:
%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> </head> <body> <form action="<c:url value='/struts2client.action'/>" method="post"> <label for="name">输入姓名:</label> <input type="text" name="name" id="name"/> <br/> <input type="submit" value="确定"/> </form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> </head> <body> <form action="<c:url value='/struts2client.action'/>" method="post"> <label for="name">输入姓名:</label> <input type="text" name="name" id="name"/> <br/> <input type="submit" value="确定"/> </form> </body> </html>
5、jsps/result.jsp页面如下
%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> </head> <body> <s:property value="name"/> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> </head> <body> <s:property value="name"/> </body> </html>
补充:Web开发 , 其他 ,