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自动生成。