ssm+cxf+webservice+jsp写一个demo
时间: 2023-09-03 21:09:07 浏览: 174
基于CXF框架的webservice的demo
好的,以下是一个简单的SSM+CXF+WebService+JSP的demo:
1. 创建一个Maven项目,添加以下依赖:
```xml
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.0</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
```
2. 配置web.xml文件:
```xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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:spring/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
```
3. 配置applicationContext.xml文件:
```xml
<beans>
<!-- 配置数据源 -->
<!-- 配置MyBatis -->
<!-- 配置Spring事务管理 -->
<!-- 配置WebService服务 -->
<bean id="helloService" class="com.example.service.HelloServiceImpl"/>
<bean id="cxf" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean">
<property name="serviceClass" value="com.example.service.HelloService"/>
<property name="address" value="/hello"/>
<property name="serviceBean" ref="helloService"/>
</bean>
</beans>
```
4. 配置dispatcherServlet.xml文件:
```xml
<beans>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.example.controller"/>
</beans>
```
5. 创建HelloService接口:
```java
@WebService
public interface HelloService {
@WebMethod
String sayHello(String name);
}
```
6. 创建HelloServiceImpl实现类:
```java
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
```
7. 创建一个Controller:
```java
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
public ModelAndView hello(String name) {
String message = helloService.sayHello(name);
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", message);
return mav;
}
}
```
8. 创建一个JSP视图,放在/WEB-INF/views/hello.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
```
9. 启动Tomcat服务器,访问http://localhost:8080/hello?name=world,应该能够看到页面上显示"Hello, world!"。
以上就是一个简单的SSM+CXF+WebService+JSP的demo,希望能对你有所帮助。
阅读全文