1. 程式人生 > >Spring+Hibernate+Struts2整合之實現登錄功能

Spring+Hibernate+Struts2整合之實現登錄功能

else ber mit generate public rac err web field

軟件152 劉安民

前端代碼:

<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" onclick="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" onclick="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>

Spring+Hibernate+Struts2整合之實現登錄功能