Spring+Hibernate+Struts2整合之实现登录功能

前端代码:

 

<form id="loginForm" action="${ pageContext.request.contextPath }/user_login.action"  method="post" novalidate="novalidate">
    <table>
	<tbody><tr>
		        <th>
			    用户名:
			</th>
			<td>
			    <input type="text" id="username" name="username" class="text" maxlength="20" οnclick="toggle('div1')";/><span><s:fielderror fieldName="username"/></span>
			</td>
		    </tr>
		    <tr>
			<th>
			    密  码:
			</th>
			<td>
			    <input type="password" id="password" name="password" class="text" maxlength="20" autocomplete="off" οnclick="toggle('div1')";/><span><s:fielderror fieldName="password"/></span>
			</td>
		    </tr>
		  <tr>
		    <td>
			<input type="submit" class="submit" value="登 录">
		    </td>
		</tr>
	</tbody></table>
</form>               

 

  

登录的action:

//前台:登录功能
    @InputConfig(resultName="loginInput")
    public String login(){        
        
        User existUser = userService.login(user);
        
        if(existUser==null){
        
            this.addActionMessage("用户名或密码错误或用户未激活!");
            return "loginInput";
        }else{
            ServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);return "loginSuccess";
        }        
    }

 

登录的service:

//业务层登录方法
    public User login(User user) {
        // TODO Auto-generated method stub
        System.out.println("用户名:"+user.getUsername()+" 密码:"+user.getPassword());
        return userDAO.login(user);
    }

 

登录的DAO:

@Override
    public User login(User user) {
        // TODO Auto-generated method stub
        String queryString = "from User where username = ? and password = ?";
        List<User> list = this.getHibernateTemplate().find(queryString,user.getUsername(),user.getPassword());
        if(list.size()!=0){
            return list.get(0);
        }
        return null;
    }

 

配置struts.xml:

<!-- 配置用户的action -->
    <action name="user_*" class="userAction" method="{1}">
        <result name="loginInput">/WEB-INF/jsp/login.jsp</result>
        <result name="loginSuccess type="redirectAction">index</result>
    </action>

 

配置applicationContext.xml:

 

<!-- 配置action -->
<bean id="userAction" class="com.ansibee.shop.web.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
</bean>

<!-- 配置service -->
<bean id="userService" class="com.ansibee.shop.service.UserService">
    <property name="userDAO" ref="userDAOImpl"></property>
</bean>

<!-- 配置Dao -->
<bean id="userDAOImpl" class="com.ansibee.shop.daoImpl.UserDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

 

转载于:https://www.cnblogs.com/ansibee/p/7073725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值