用SSH实现登录注册
1、相关环境和技术:
- IDE:eclipse
- 数据库:MySQL,Navicat
- 单元测试:JUnit
- Struts2+Hibernate5+spring4
2、新建一个Dynamic Web project及搭建SSH环境
3、数据库
4、封装JavaBean和编写映射文件
package hnt;
public class Userinfo {
private int id ;
private String username;
private String pwd;
private String email;
public Userinfo(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "Userinfo [id=" + id + ", username=" + username + ", pwd=" + pwd + ", email=" + email + "]";
}
}
配置文件:Userinfo.hbm.xml
<hibernate-mapping>
<class name="hnt.Userinfo" table="USERINFO">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="pwd" type="java.lang.String">
<column name="PWD" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
</class>
</hibernate-mapping>
———–由于hibernate交由Spring管理,所以不需要cfg.xml
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 解决延迟加载的问题 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<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>
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="crm" extends="struts-default" namespace="/">
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<!-- 通配符配置--!>
<action name="user_*" class="userAction" method="{1}">
<resultname="loginok"type="redirect">/bookstore.jsp
</result>
<allowed-methods>add,checkCode,login,exit</allowed-methods>
</action>
</package>
</struts>
5、Action