show_sql 是在进行CRUD操作时控制台输出该HQL语句 format_sql 是格式化HQL语句 hbm2ddl.auto 自动建表语句 connection.url 连接数据库URL connection.username connection.password
连接数据库用户名 连接数据库密码
4.2.2 Struts配置文件--------- struts-config.xml
struts-config.xml文件,是由ActionServlet读取的配置文件,它定义了所有关于Action、ActionForm、ActionForward等的详细信息。
主要代码如下:
type=\
type=\ scope=\ >
……《代码省略》 主要部分解释: 其中
? name属性是给此ActionForm一个标识名称;
17
? type属性指定了此ActionForm是哪个类,必须是全路径的类名。
我们在struts-config.xml文件中,紧接着
? path-从页面上通过一个什么样的URL路径来访问Action(不包含.do); ? type – 访问这个URL的时候,调用哪个Action类,这是Action的全路径类名 ? name – 这个属性用来标识哪个ActionForm将被创建,并将提交的表单组件给它; ? scope – FormBean的作用域范围,可以取值为session和request,一般取值都是
request。
此时LoginAction应用程序的流程如下图:
4.2.3 数据库的链接
在dbUtil包下面新建connectionFactory.java文件,其主要代码如下: public class ConnectionFactory {
18
login_success.jsplogin_error.jsp响应response响应responseLoginActionForm.class创建对象并设值login.jsp提交表单ActionServlet创建对象并调用LoginAction.class读取配置文件struts-cofig.xml
static{
InputStream in=null; try {
ClassLoader loader=ConnectionFactory.class.getClassLoader(); in=loader.getResourceAsStream(\
private static Properties config=new Properties();
//读取db.properties文件中的配置数据
config.load(in);
} catch (IOException e) {
e.printStackTrace();
}finally{ } }
try {
in.close();//
} catch (IOException e) { }
e.printStackTrace();
建立一个链接工厂的类,方便了项目中代码的对数据库的调用。
19
4.3 各项功能的实现 4.3.1 新用户注册
主要代码如下:
RegisterCustomerActionForm customerForm=(RegisterCustomerActionForm)form;
Customer customer=null;
customer=customerForm.createCustomer();
CustomerRegisterService sc=new CustomerRegisterService(); if(sc.save(customer)){
request.getSession().setAttribute(\return mapping.findForward(\
}else{ }
return mapping.findForward(\
20