首先需要导入hibernate和struts的核心包,SQL Server2008数据库连接包,没有需要去官网下载

接着web.xml配置,主要代码如下,以struts 2.5.16配置为例:
<?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" version="3.1">
<display-name>HibernateDemo1</display-name>
<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>
在src文件新建包 com.bean,包中新建类Student.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
int id;
String name;
String password;
public Student() {
// TODO 自动生成的构造函数存根
}
//需要设置主键,对应数据库里的主键
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID", unique=true, nullable=false, insertable=true, updatable=false, precision=20, scale=0)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;

这篇博客介绍了如何结合Hibernate和Struts框架来实现简单的用户注册和登录功能。首先,文章提到了需要导入相关库,然后配置web.xml文件以设定Struts2过滤器。接着,创建了实体类Student及其映射文件,并配置了Hibernate的数据库连接信息。在actions包中创建了处理注册和登录请求的类,并通过struts.xml进行路径映射。最后,展示了register.jsp、login.jsp和login_success.jsp三个页面的代码,完成了用户界面的展示。
最低0.47元/天 解锁文章
5827

被折叠的 条评论
为什么被折叠?



