一个用SSH框架开发的简单的登录注册系统

  这是我用SSH框架开发的一个简单的注册登录系统,包括js校验,css前端样式呈现等等;下面来详细介绍系统的开发流程:


软硬件平台

  软件环境

1.win7 32为操作系统;

2.MyEclipse10.5;

3.Tomcat7.0;

4.Java EE6;

5.jdk1.7;

6.MySQL 5.6;

  硬件环境

CPU:Intel Pentium Dual-Core T4500 2.3GHZ处理器;

内存:2G;


项目所依赖的包:Hibernate3.0;Spring3.0;Struts2;struts-dojo-plugin-2.3.1.2;struts-spring-plugin-2.0.11.2;


项目的总体架构图如下:



以下为前端的呈现界面:












以下为核心代码:

其中,将com.yile.vo包下的Users实体类的代码粘贴如下:

package com.yile.vo;


import java.io.Serializable;


public class User implements Serializable {
private Integer id;
private String userName;
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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;
}

}


下面将com.yile.action包下的UserAction类的代码粘贴如下:

package com.yile.action;


import java.util.List;


import com.opensymphony.xwork2.ActionSupport;
import com.yile.service.UserService;
import com.yile.vo.User;


@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
private String role;
private String msg;
private User user;
private List<User> users;
private UserService userService;


public String execute() {
return SUCCESS;
}


public String login() {
if(role.equals("admin")){
//System.out.println("role="+role);//测试数据
if(user.getUserName().equals("admin")){
User adminUser=userService.queryUserByName("admin");
if(adminUser.getPassword().equals(user.getPassword())){
msg="登录成功!";
   return "admin";
}else{
msg="管理员密码错误,你没有权限登录!";
return "failure";
}
}else{
msg="非管理员,你无权限查看用户记录!";
return "failure";
}
}else{
if(user.getUserName().equals("admin")){
msg="这是普通用户登录界面,请管理员到管理员界面登录!";
return "failure";
}else if(userService.queryUser(user.getUserName(),user.getPassword())){
msg="登录成功!";
return "user";
}else{
msg="用户名或密码错误,请重新填写后登录!";
return "failure";
}
}
}


public String queryAllUser(){
users=userService.queryAll();
return SUCCESS;
}

public String addUser() {
users=userService.queryAll();
System.out.println("size="+users.size());
for(int i=0;i<users.size();i++){
if(user.getUserName().equals(users.get(i).getUserName())){
msg="当前用户已存在!请用其它用户名注册!";
return "addFailure";
}
}
boolean b=userService.addUser(user);
if(b){
msg="注册成功!";
return "addSuccess";
}else{
msg="注册失败!";
return "addFailure";
}
}


public String deleteUser(){
System.out.println("id="+user.getId());
try{
userService.deleteUserById(user.getId());//测试数据
msg="删除成功!";
return SUCCESS;
}catch(Exception e){
return INPUT;
}
}

public String editUser(){
//System.out.println("actionid="+user.getId()+user.getUserName());
userService.updateUser(user);
msg="修改成功!";
this.setRole("admin");
return SUCCESS;
}

public String queryUserById(){
//System.out.println("id="+user.getId());
user=userService.getUserById(user.getId());
return SUCCESS;
}

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

}

以上基本完成后台代码编程,另外还有一些包及类请参看架构缩略图,自行编写。另外有一些orm类的编程,在这里也不再介绍。

以下介绍SSH的关键配置文件,关于Struts2的struts.xml,struts.properties;关于Spring3.3的applicantionContext.xml的配置;关于Hibernate3.0的hibernate.cfg.xml的配置及对象关系映射文件User.hbm.xml的配置;各个配置文件均放在classes目录下,即放在src根目录下。但User.hbm.xml放在com.yile.vo包下;


以下为各个配置文件的详细配置信息:

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="struts2" extends="struts-default" namespace="/">

<action name="Login" class="userAction" method="login">
   <result name="admin" type="redirectAction">ListUsers</result>
   <result name="user">/global/success.jsp</result>
   <result name="failure">/global/failure.jsp</result>
   <result name="input">/index.jsp</result>
</action>

<action name="Register" class="userAction" method="addUser">
   <result type="redirect">/users/register.jsp</result>
   <result name="addSuccess">/global/success.jsp</result>
   <result name="addFailure">/global/failure.jsp</result>
</action>

<action name="ListUsers" class="userAction" method="queryAllUser">
   <result name="success">/admin/listUsers.jsp</result>
</action>

<action name="DeleteUser" class="userAction" method="deleteUser">
<result name="success">/global/success.jsp</result>
<result name="input">/global/failure.jsp</result>
</action>

<action name="EditUser" class="userAction" method="editUser">
   <result name="success">/global/success.jsp</result>
   <result name="input">/global/failure.jsp</result>
</action>

<action name="QueryById" class="userAction" method="queryUserById">
   <result>/admin/editUser.jsp</result>
</action>

</package>
</struts>


struts.properties的详细配置信息:

struts.i18n.encoding=utf-8
struts.devMode=true
struts.locale=en_US
struts.objectFactory=spring
struts.objectFactory.spring.autoWire=name
struts.objectFactory.spring.useClassCache=true
struts.objectTypeDeterminer=tiger
struts.enable.DynamicMethodInvocation=true
struts.tag.altSyntax=true
struts.url.includeParams=none
struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper
struts.action.extension=do
struts.serve.static.browserCache=false
struts.i18n.reload=true
struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager

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        
       classpath:/org/springframework/beans/factory/xml/spring-beans-2.0.xsd"  
             default-lazy-init="true">
<bean id="proxyTemplate" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

    <!-- 声明式事务管理 配置DAO -->
<bean id="nameproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>userDao</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>proxyTemplate</value>
            </list>
        </property>
</bean>
    
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation" value="classpath:hibernate.cfg.xml">
   </property>
</bean>

<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory">
   </property>
</bean>

<bean name="userAction" class="com.yile.action.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>

<bean name="userService" class="com.yile.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

<bean name="userDao" class="com.yile.dao.impl.UserDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>


hibernate.cfg.xml的详细配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration> 
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/regandlog</property>
<property name="connection.username">yile</property>
<property name="connection.password">19910223yyl</property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">org.hibernate.context.ManageredSessionContext</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/yile/vo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


User.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">
<hibernate-mapping package="com.yile.vo">
<class name="User" table="user">
<id name="id" column="Id">
<generator class="native"/>
</id>
<property name="userName" column="UserName"/>
<property name="password" column="Password"/>
</class>
</hibernate-mapping>

至于jsp文件和js校验代码,在这里就不再详细介绍了,如果有哪位想要全部代码,可到我上传的资源处下载。

猜你喜欢

转载自blog.csdn.net/y2010081134/article/details/28112407