项目背景:集成OCX视频监控控件,Java项目版本是Spring+Struts2,故先做个试验。
Idea集成Spring+Struts2项目。文章参考:https://www.cnblogs.com/grasp/p/11090136.html
项目源码下载地址:https://download.csdn.net/download/boonya/11997055
目录
创建项目
选择spring和Struts2
最终项目文件如下:
web.xml配置
默认生成的filter是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter去掉ng。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
JSP页面配置
WEB-INF/index.jsp
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2019/11/25
Time: 16:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="Login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
WEB-INF/jsp/success.jsp
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2019/11/25
Time: 16:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${username}登录成功</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
Spring 配置
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="loginAction" class="com.spring.struts2.demo.action.LoginAction" scope="prototype"/>
</beans>
Struts2配置
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="com.spring.struts2.demo.action.LoginAction">
<result name="success">/WEB-INF/jsp/success.jsp</result>
<result name="login">/index.jsp</result>
</action>
</package>
</struts>
Action配置
LoginAction.java
package com.spring.struts2.demo.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 登录action
*/
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
if (username.equals("admin") && password.equals("123456")) {
return SUCCESS;
} else {
return LOGIN;
}
}
}
项目环境配置
jdk版本
Spring依赖包:红框内的需要额外下载添加。
Struts2包需要下载替换包:默认下载的包没有版本号。下载地址:https://struts.apache.org/download.cgi
设置目标输出
记得fix一下Struts2。
Tomcat配置
运行项目:
成功页面:
错误排除参考:https://blog.csdn.net/happywlg123/article/details/81389772
更进一步实践实现数据访问: