SSH实现分用户登录

login.jsp

<form id="loginForm" name="login" method="post"  action="<span style="color:#FF0000;">checkAccount</span>">  //与struts.xml中的action name 属性对应,将此form的数据提交到checkAccount中
        <input id="User" name="<span style="color:#FF0000;">account</span>" placeholder="登录账号"  />   //name属性跟AccountServiceAction.java中的变量对应,否则获取不到页面值
        <input type="password" id="Pwd" name="<span style="color:#FF0000;">accountPwd</span>" placeholder="登录密码" />
        <s:fielderror fieldName="accountPwd"/> 
        <button id="submit" >登录</button>
        <button id="forget">忘记密码</button>
        <br/>
        </form>


AccountServiceAction.java

public class AccountServiceAction extends ActionSupport {  //使用execute需要继承ActionSupport类

	private String account;
	private String accountPwd;
	private Boolean ifChange;
	private Boolean ifAdmin;  //是否管理员,数据库设置相应检测字段
	
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	AccountDao accountDao =(AccountDao)ctx.getBean("accountDao");

	...get/set...

        public String execute()throws Exception{
		List<Account>accountList=accountDao.findByName(account);
		
		for(Account account:accountList){
			if(account.getAccountPwd().equals(accountPwd)){
				//获取session
				Map<String,Object>session = ActionContext.getContext().getSession();
				//加入session值
				session.put("account", account);
				
			  if(account.getIfAdmin()){
				  return LOGIN;
			  }else{
				  if(account.getIfChange()){
					  return SUCCESS;
				  }else{
					  return INPUT;
				  }
			  }
			  
		    }else{
				addFieldError("accountPwd","密码错误!");
				return ERROR;
				}
		}
		return ERROR;
	}
}

struts.xml

<action name="<span style="color:#FF0000;">checkAccount</span>" class="com.scholar.action.AccountServiceAction" >
     <result name="success">/index.jsp</result> 
     <result name="error">/login.jsp</result>
     <result name="login">/background.jsp</result> 
     <result name="input">/register.jsp</result>  
   </action>


applicationContext.xml

<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					<span style="color:#FF0000;">org.hibernate.dialect.SQLServerDialect</span> //设置数据库方言,不同数据库不一样
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/scholar/Account/<span style="color:#FF0000;">Account.hbm.xml</span></value> //说明映射文件位置
				
			</list>
		</property>
	</bean>
	
	<!-- HibernateTemplate类是简化Hibernate数据库访问代码的辅助类,可以获取一个Session对象 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
	   <property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<bean id="<span style="color:#FF0000;">accountDao</span>"  //id名设为实例名 AccountDao <span style="color:#FF0000;">accountDao</span> =(AccountDao)ctx.getBean("accountDao");
		class="com.scholar.imp.AccountDaoImp" > //说明接口的实现类
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
web.xml

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value><span style="color:#FF0000;">classpath:applicationContext.xml</span></param-value>  //放在WEB-INF下
  </context-param>


AccountDao.java

public interface AccountDao {
	
	void saveOrUpdate(Account account);
	
	List<Account>findByName(String account);
	
}

AccountDaoImp.java

public class AccountDaoImp implements AccountDao {
	//实例化一个hibernateTemplate对象,用于执行持久化操作
	private static HibernateTemplate ht=null;

        //hibernateTemplate持久化操作所需SessionFactory
	private SessionFactory sessionFactory;  

    //依赖注入SessionFactory 的必需的setter 方法  
    public void setSessionFactory(SessionFactory sessionFactory)  
     {  
        this.sessionFactory = sessionFactory;  
    }  
    //该方法用于完成HibernateTemplate的初始化  
    private HibernateTemplate getHibernateTemplate()      
     {  
         if (ht ==null){  
            ht = new HibernateTemplate(sessionFactory);  
        }  
       return ht;
    }  

	@Override
	public void saveOrUpdate(Account account) {
		getHibernateTemplate().saveOrUpdate(account);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> findByName(String account) {
		String sql="<span style="color:#FF0000;">from Account u where u.account= ?</span>"; //用HQL语句,这个是hibernate提供的一种sql语句
  		return this.getHibernateTemplate().find(sql,account);
	}
}

Account.java和Account.hbm.xml自动生成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值