SSH框架之最佳实践

前面时间,学习了如何通过SSH框架开发实际的项目,下面就来进行SSH框架的最佳实践。总共会介绍5个最佳实践,由易到难。一步一步实现
[img]http://dl.iteye.com/upload/attachment/0066/8547/d85a0b2d-5c1d-3cb8-815e-8a4d47f9aa78.jpg[/img]


[b]第一个最佳实践,MVC管理,实现简单登录[/b]
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的文件

[b]类名:UserAction[/b]

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


}



[b]配置struts-user.xml文件,实现userLogin[/b]

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


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

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


[b]jsp页面[/b]

【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>


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

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

[b]数据库设计[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值