ssm+cxf+webservice+jsp完成一个登录的demo
时间: 2023-08-18 13:06:52 浏览: 152
好的,我可以为您提供一个简单的示例。
1. 创建一个新的Maven项目,添加如下依赖:
```xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.4</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- Other dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
```
2. 创建一个新的Spring配置文件 applicationContext.xml,添加如下内容:
```xml
<!-- 配置CXF -->
<bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus">
<property name="cxfConfigLocation" value="classpath:META-INF/cxf/cxf.xml"/>
</bean>
<!-- 配置WebService -->
<jaxws:endpoint
id="userService"
implementor="com.example.service.impl.UserServiceImpl"
address="/UserService"/>
<!-- 配置Spring MVC -->
<context:component-scan base-package="com.example.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
```
3. 创建一个新的CXF配置文件 cxf.xml,添加如下内容:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:tns="http://example.com/service">
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<cxf:bus>
<cxf:features>
<wsdl:logging/>
</cxf:features>
</cxf:bus>
<jaxws:endpoint
xmlns:tns="http://example.com/service"
id="userService"
implementor="com.example.service.impl.UserServiceImpl"
address="/UserService">
<jaxws:serviceFactory>
<ref bean="cxf"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
```
4. 创建一个新的Java类 UserService,添加如下内容:
```java
package com.example.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface UserService {
@WebMethod
public boolean login(@WebParam(name = "username") String username,
@WebParam(name = "password") String password);
}
```
5. 创建一个新的Java类 UserServiceImpl,添加如下内容:
```java
package com.example.service.impl;
import javax.jws.WebService;
import com.example.service.UserService;
@WebService(endpointInterface = "com.example.service.UserService")
public class UserServiceImpl implements UserService {
@Override
public boolean login(String username, String password) {
// Your login logic here
return true;
}
}
```
6. 创建一个新的Spring MVC控制器 UserController,添加如下内容:
```java
package com.example.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.service.UserService;
@Controller
public class UserController {
private static final String SERVICE_URL = "http://localhost:8080/demo/UserService?wsdl";
private static final QName SERVICE_NAME = new QName("http://example.com/service", "UserService");
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
// Create a new Service instance
Service service = Service.create(SERVICE_URL, SERVICE_NAME);
// Get the UserService instance from the Service
UserService userService = service.getPort(UserService.class);
// Call the login method
boolean result = userService.login(username, password);
// Return the result
ModelAndView mav = new ModelAndView();
if (result) {
mav.setViewName("success");
} else {
mav.setViewName("error");
}
return mav;
}
}
```
7. 创建一个新的JSP视图 success.jsp,添加如下内容:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Success</title>
</head>
<body>
<h1>Login Success</h1>
</body>
</html>
```
8. 创建一个新的JSP视图 error.jsp,添加如下内容:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Error</title>
</head>
<body>
<h1>Login Error</h1>
</body>
</html>
```
9. 在Web.xml中添加如下内容:
```xml
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
10. 运行项目,并在浏览器中访问 http://localhost:8080/demo/login,输入用户名和密码,点击登录按钮,可以看到登录成功或失败的页面。
希望这个示例能对您有所帮助。
阅读全文