生成用户表:
create table t_user( user_id int auto_increment not null, username varchar(10) not null, password varchar(15) not null, email varchar(20) , is_webmaster char(1) not null default '0', is_administrator char(1) not null default '0', primary key (user_id), UNIQUE KEY user (username) )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; insert into t_user(username,password,email) values('limx','limx','[email protected]'); insert into t_user(username,password,email) values('messi','messi','[email protected]');
工程web.xml配置文件:
<?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"> <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.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.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" namespace="" extends="struts-default">
<action name="register" class="bbs.action.RegisterAction">
<result name="success">jsp/user/login.jsp</result>
<result name="input">jsp/user/register.jsp
</result>
</action>
</package>
</struts>
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-2.5.xsd"> <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/forum"> </property> <property name="username" value="root"></property> <property name="password" value="limx"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="DataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>forum/model/TUser.hbm.xml</value> </list> </property> </bean> <bean id="TUserDAO" class="forum.dao.TUserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans>
TUser.hbm.xml配置文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="forum.model.TUser" table="t_user" catalog="forum"> <id name="userId" type="java.lang.Integer"> <column name="user_id" /> <generator class="identity" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="10" not-null="true" unique="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="15" not-null="true" /> </property> <property name="email" type="java.lang.String"> <column name="email" length="20" /> </property> <property name="isWebmaster" type="java.lang.String"> <column name="is_webmaster" length="1" not-null="true" /> </property> <property name="isAdministrator" type="java.lang.String"> <column name="is_administrator" length="1" not-null="true" /> </property> </class> </hibernate-mapping>
TUser.java
package forum.model; import java.util.HashSet; import java.util.Set; /** * TUser entity. @author MyEclipse Persistence Tools */ public class TUser implements java.io.Serializable { // Fields private Integer userId; private String username; private String password; private String email; private String isWebmaster; private String isAdministrator; // Constructors /** default constructor */ public TUser() { } /** minimal constructor */ public TUser(String username, String password, String isWebmaster, String isAdministrator) { this.username = username; this.password = password; this.isWebmaster = isWebmaster; this.isAdministrator = isAdministrator; } /** full constructor */ public TUser(String username, String password, String email, String isWebmaster, String isAdministrator) { this.username = username; this.password = password; this.email = email; this.isWebmaster = isWebmaster; this.isAdministrator = isAdministrator; } // Property accessors public Integer getUserId() { return this.userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getIsWebmaster() { return this.isWebmaster; } public void setIsWebmaster(String isWebmaster) { this.isWebmaster = isWebmaster; } public String getIsAdministrator() { return this.isAdministrator; } public void setIsAdministrator(String isAdministrator) { this.isAdministrator = isAdministrator; } }
RegisterAction.java
package bbs.action; import forum.dao.TUserDAO; import forum.model.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class RegisterAction extends ActionSupport implements ModelDriven { private TUser user = new TUser(); public Object getModel() { return user; } public String execute() { // TUserDAO userDAO = new TUserDAO(); // suserDAO.save(user); ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); TUserDAO dao = (TUserDAO) context.getBean("TUserDAO"); try { dao.save(user); } catch (Exception e) { return INPUT; } return SUCCESS; } }
Register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="register" method="post"> <s:textfield label="username" name="username" value="" ></s:textfield><br> <s:password label="password" name="password" value=""> </s:password><br> <s:textfield label="email" name="email" value=""> </s:textfield><br> <s:submit value="register" align="left"></s:submit> </form> </body> </html>
ps:因为是从工程里面拿出来修改的,所以有可能出现错误,需要工程源码的话,再找我要;