Project目录
主页-index.jsp
- 与国际化和本地化有关的标签:<s:form>...</s:form>
- <s:>标签中的key属性与资源文件(properties)中对应
<body>
<s:form action="Login">
<s:textfield name="username" key="user"/><!--key="user"对应“用户名”-->
<s:password name="password" key="pass"/>
<s:submit key="login"/>
</s:form>
<s:url id="localeenUS" namespace="/" action="Locale" >
<s:param name="request_locale" >en_US</s:param>
</s:url>
<s:url id="localezhCN" namespace="/" action="Locale" >
<s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:a href="%{localeenUS}" >English</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
</body>
mess.properties
action(struts)
- 继承自ActionSupport类
- action类中的成员变量与jsp页面提交的表单中的属性同名
LoginAction.java
package ghn.action;
//与struts有关的包
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
//与hibernate有关的包
import ghn.utils.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class LoginAction extends ActionSupport
{
//注意这里定义的变量与login.jsp中的
private String username;
private String password;
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
public String execute() throws Exception
{
ActionContext ctx = ActionContext.getContext();
Session s = HibernateUtil.currentSession();
Transaction tx=s.beginTransaction();
String hql= "From ghn.domain.User u where u.username=? and u.password=?";
List lst = (List)s.createQuery(hql).setString(0, username).setString(1,password).list();
tx.commit();
HibernateUtil.closeSession();
if (lst.size()>0)
{
ctx.getSession().put("user" , getUsername());//sessionScope.user
ctx.put("tip" , getText("succTip"));//requestScope.tip
return "success";
}
else
{
ctx.put("tip" , getText("failTip"));
return "error";
}
}
}
struts.xml
- 包括用于国际化的资源文件信息:<constant name="struts.custom.i18n.resources" value="mess"/>
- 项目中的action类的信息:哪个类?成功或失败跳转的页面(e.g.
/.welcom.jsp)?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="mess"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="ghn" extends="struts-default">
<action name="Login" class="ghn.action.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
<action name="Locale" class="ghn.action.LocaleAction">
<result name="SUCCESS">/login.jsp</result>
</action>
<action name="">
<result>.</result>
</action>
</package>
</struts>
Hibernate
hibernate.cfg.xml
- 待持久化类的位置:<mapping class="ghn.domain.User"/>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">******</property>
<!-- j2ee是数据库名 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/j2ee</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="ghn.domain.User"/>
</session-factory>
</hibernate-configuration>
User.java 持久化类
package ghn.domain;
import javax.persistence.*;
@Entity
@Table(name="User")
public class User {
@Id
private long id;
@Column (name = "username")
private String username;
@Column (name = "password")
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MySql
选择数据库 use j2ee;
创建表格(注意是反引号) create table User(`id` int,`username` varchar(30),`password` varchar(10), primary key(`id`));
插入记录 insert into User (201601,'CCNU','ccnu123');
HibernateUtil.java
package ghn.utils;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil
{
public static final SessionFactory sessionFactory;
static
{
try
{
//采用默认的hibernate.cfg.xml来启动一个Configuration的实例
Configuration configuration = new Configuration()
.configure();
//由Configuration的实例来创建一个SessionFactory实例
sessionFactory = configuration.buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
//ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步
public static final ThreadLocal<Session> session
= new ThreadLocal<Session>();
public static Session currentSession()
throws HibernateException
{
Session s = session.get();
//如果该线程还没有Session,则创建一个新的Session
if (s == null)
{
s = sessionFactory.openSession();
//将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}
public static void closeSession()
throws HibernateException
{
Session s = session.get();
if (s != null)
s.close();
session.set(null);
}
}
注意事项
- jar包放在webcontent/web-inf/lib下
网友评论