SSH框架之最佳实践

前面时间,学习了如何通过SSH框架开发实际的项目,下面就来进行SSH框架的最佳实践。总共会介绍5个最佳实践,由易到难。一步一步实现

第一个最佳实践,MVC管理,实现简单登录

1.数据库设计,tb_user

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `userId` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(33) DEFAULT NULL,
  `userPass` varchar(33) DEFAULT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO tb_user VALUES ('1', 'sa', '123');

2.编写loginAction,同时,配置loginAction的struts文件,以及spring管理loginAction的文件

类名:UserAction

package com.neweducation.user.action;

import java.io.File;

import com.core.base.action.BaseAction;
import com.neweducation.user.model.UserModel;
import com.neweducation.user.service.UserService;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends BaseAction implements ModelDriven<UserModel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserService userService;
	
	private UserModel userModel = new UserModel();//采用驱动模型
	/**
	 * 登录
	 * @return
	 */
	public String userLogin() {
		UserModel user = userService.loginJudge(userModel.getUserName(),
				userModel.getUserPass());
		if (user != null) {			 
			getSession().setAttribute("user", user);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

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

	public UserService getUserService() {
		return userService;
	}

	@Override
	public UserModel getModel() {
		return getUserModel();
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

			
	}

配置struts-user.xml文件,实现userLogin

<package name="user" extends="json-default" >
        <action name="userLogin" class="UserAction" method="userLogin">
            <result name="success">index.jsp</result>
            <result name="input">login.jsp</result>
        </action>
    
    </package>

配置spring-bean-user.xml文件,实现对UserAction的管理

<bean id="UserAction" class="com.neweducation.user.action.UserAction"
		scope="prototype">
		<property name="userService" ref="userService" /></bean>
	<bean id="userService" class="com.neweducation.user.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao" />		
	</bean>

	<bean id="userDao" class="com.neweducation.user.dao.UserDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

jsp页面

【login.jsp】

<form name="loginform" action="userLogin" method="post">
		用户名:<input type="text" name="userName"><br>
		密码:<input type="password" name="userPass"><br>
		<input type="submit" value="登录"> 
	</form>

【-------------------------------------------------------------------】

第二个最佳实践,系统权限管理,控制用户访问模块

数据库设计

相关推荐