工具:eclipse+mysql
SSH版本:struts-2.3.16.1 spring-framework-4.2.2.RELEASE hibernate-release-5.2.2.Final
本人观看视频并按照视频步骤后写出的,视频链接:点击打开链接,侵权必删
一、导入jar包
新建web项目“ssh”,默认点击“下一步”,在最后勾选“web.xml”复选框
在src目录中新建四个包,com.ssh.action,com.ssh.dao,com.ssh.entity,com.ssh.service
百度“SSH整合jar包”下载,将其导入“lib”文件夹中
二、搭建Struts2
在com.ssh.action包中新建“UserAction.java”
public class UserAction extends ActionSupport {
public String login() {
return "login";
}
}
在src目录下新建Struts2核心配置文件“struts.xml”,并配置“UserAction.java”
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default" >
<action name="user_login" class="com.ssh.action.UserAction" method="login">
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
在“web.xml”中配置struts2过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts2搭建完成
三、搭建Hibernate
在com.ssh.entity中创建实体类“User.java”
public class User {
private int uid;
private String username;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
在com.ssh.entity中创建“User.hbm.xml”
<?xml version="1.0" encoding="UTF-8"?>
<