javascript

位置:IT落伍者 >> javascript >> 浏览文章

整合 JSF+Spring+Hibernate的小例子


发布日期:2018年12月11日
 
整合 JSF+Spring+Hibernate的小例子

()UserInfojava


packagecomzhaoqingjieservicehibernate;

importjavaioSerializable;

importmonslangbuilderToStringBuilder;

publicclassUserInfoimplementsSerializable{

privateStringid;

privateStringusername;

privateStringemail;

publicUserInfo(StringidStringusernameStringemail){

thisid=id;

thisusername=username;

thisemail=email;

}

publicUserInfo(){

}

publicUserInfo(Stringid){

thisid=id;

}

publicStringgetId(){

returnid;

}

publicvoidsetId(Stringid){

thisid=id;

}

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

thisemail=emial;

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

thisusername=username;

}

publicStringtoString(){

returnnewToStringBuilder(this)append(idgetId())toString();

}

}

()UserInfohbmxml


<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

mappingdtd>

<hibernatemapping>

<classname=comzhaoqingjiespringservicehibernateUserInfotable=userinfo>

<metaattribute=classdescriptioninherit=false>

@hibernateclass

table=userinfo

</meta>

<idname=idtype=javalangStringcolumn=id>

<metaattribute=fielddescription>

@hibernateid

generatorclass=assigned

type=javalangString

column=id

</meta>

<generatorclass=assigned/>

</id>

<propertyname=usernametype=javalangStringcolumn=usernamelength=>

<metaattribute=fielddescription>

@hibernateproperty

column=username

length=

</meta>

</property>

<propertyname=emailtype=javalangStringcolumn=emaillength=>

<metaattribute=fielddescription>

@hibernateproperty

column=email

length=

</meta>

</property>

</class>

</hibernatemapping>

()IUserInfoDAOjava

packagecomzhaoqingjiespringservicedao;

importcomopenvspringdomainmodelUserInfoVO;

importorgspringframeworkdaoDataAccessException;

importjavaioSerializable;

publicinterfaceIUserInfoDAOextendsSerializable{

publicbooleansetUserInfo(UserInfoVOuserinfoVO)throwsDataAccessException;

}

()UserInfoDAOjava

packagecomzhaoqingjiespringservicedaoimpl;

importmonsloggingLog;

importmonsloggingLogFactory;

importorgdoomdarkuuidUUIDGenerator;

importorgspringframeworkdaoDataAccessException;

importorgspringframeworkormhibernatesupportHibernateDaoSupport;

importcomzhaoqingjiespringdomainmodelUserInfoVO;

importcomzhaoqingjiespringservicedaoIUserInfoDAO;

importcomzhaoqingjiespringservicehibernateUserInfo;

publicclassUserInfoDAOextendsHibernateDaoSupportimplementsIUserInfoDAO{

privatestaticfinalLoglog=LogFactorygetLog(UserInfoDAOclass);

publicbooleansetUserInfo(UserInfoVOuserinfoVO)throwsDataAccessException{

if(userinfoVO==null){

returnfalse;

}

UserInfoui=newUserInfo();

uisetId(getID());

uisetUsername(userinfoVOgetUsername()trim());

uisetEmail(userinfoVOgetEmail()trim());

thisgetHibernateTemplate()save(ui);

returntrue;

}

privateStringgetID(){

returnUUIDGeneratorgetInatance()generateTimeBaseUUID()toString();

}

}

()IExampleManagerjava


packagecomzhaoqingjiespringservice;

importcomopenvspringdomainmodelUserInfoVO;

importorgspringframeworkdaoDataAccessException;

importjavaioSerializable;

publicinterfaceIExampleManagerextendsSerializable{

publicbooleansetUserInfo(UserInfoVOuserinfoVO)

throwsDataAccessException;

}

()ExampleManagerImpljava

packagecomzhaoqingjiespringserviceimpl;

importcomopenvspringdomainmodelUserInfoVO;

importcomopenvspringserviceIExampleManager;

importcomopenvspringservicedaoIUserInfoDAO;

importmonsloggingLog;

importmonsloggingLogFactory;

importorgspringframeworkdaoDataAccessException;

publicclassExampleManagerImplimplementsIExampleManager{

privatestaticfinalLoglog=LogFactory

getLog(ExampleManagerImplclass);

privateIUserInfoDAOuserinfo;

publicExampleManagerImpl(){

(ExampleManagerImpl());

}

publicvoidsetUserinfo(IUserInfoDAOuserinfoDAO)

throwsDataAccessException{

thisuserinfo=userinfoDAO;

}

publicbooleansetUserInfo(UserInfoVOuserinfoVO)

throwsDataAccessException{

returnuserinfosetUserInfo(userinfoVO);

}

}

()UserInfoVOjava

packagecomzhaoqingjiespringdomainmodel;

importjavaioSerializable;

publicclassUserInfoVOimplementsSerializable{

privateStringusername;

privateStringemail;

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

thisemail=email;

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

thisusername=username;

}

}

JSF表示层

()InfoBeanjava


packagecomzhaoqingjiespringjsf;

importntextFacesContext;

importmonsloggingLog;

importmonsloggingLogFactory;

importorgntextApplicationContext;

importorgspringframeworkwebjsfFacesContextUtils;

importcomopenvspringdomainmodelUserInfoVO;

importcomopenvspringserviceIExampleManager;

publicclassInfoBean{

privatestaticfinalLoglog=LogFactorygetLog(InfoBeanclass);

privateStringusername=null;

privateStringemail=null;

privateStringresponse=null;

privatelongmaximum=;

privatebooleanmaximumSet=false;

privatelongminimum=;

privatebooleanminimumSet=false;

publicInfoBean(){

}

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

thisemail=email;

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

thisusername=username;

}

publicStringsubmitPersonInfo(){

(username);

(email);

ApplicationContextac=FacesContextUtils

getWebApplicationContext(FacesContextgetCurrentInstance());

IExampleManagerem=(IExampleManager)ac

getBean(exampleService);

UserInfoVOuiVO=newUserInfoVO();

uiVOsetUsername(username);

uiVOsetEmail(email);

booleanflag=emsetUserInfo(uiVO);

if(flag){

setResponse(注册成功);

returnsuccess;

}else{

setResponse(注册失败);

returnfailure;

}

}

publicvoidsetResponse(Stringresponse){

thisresponse=response;

}

publicStringgetResponse(){

returnnull;

}

publiclonggetMaximum(){

return(thismaximum);

}

publicvoidsetMaximum(longmaximum){

thismaximum=maximum;

thismaximumSet=true;

}

publiclonggetMinimum(){

return(thisminimum);

}

publicvoidsetMinimum(longminimum){

thisminimum=minimum;

thisminimumSet=true;

}

}

()indexjsp


<html>

<head>

</head>

<body>

<jsp:forwardpage=/example/homejsp/>

</body>

</html>

()homejsp

<%@pagecontentType=text/html;charset=gbk%>

<%@tagliburi=prefix=h%>

<%@tagliburi=prefix=f%>

<html>

<head>

<title>

用户注册

</title>

</head>

<br>

<f:view>

<h:formid=helloForm>

<tableborder=align=center

bordercolor=#CCcellpadding=bordercolorlight=#>

<tr>

<tdcolspan=bgcolor=#CCFF>输入用户注册信息</td>

</tr>

<tr>

<td>

<divalign=right>用户名</div>

</td>

<td>

<h:inputTextid=usernamevalue=#{InfoBeanusername}>

<f:validateLengthminimum=#{InfoBeanminimum}

maximum=#{InfoBeanmaximum}/>

</h:inputText>

</td>

</tr>

<tr>

<td>

<divalign=right>E_mail</div>

</td>

<td>

<h:inputTextid=emailvalue=#{InfoBeanemail}/>

</td>

</tr>

<tr>

<tdcolspan=bgcolor=#FFFF>

<span>

<h:messageid=message

for=username/></span>

</td>

</tr>

<tr>

<tdalign=centercolspan=>

<h:commandButtonid=submit

action=#{InfoBeansubmitPersonInfo}value=提交/>

</td>

</tr>

</table>

</h:form>

</f:view>

</html>

()successjsp

<%@pagecontentType=text/html;charset=gbk%>

<%@tagliburi=prefix=h%>

<%@tagliburi=prefix=f%>

<html>

<head>

<title>

用户注册成功

</title>

</head>

<bodybgcolor=white>

<f:view>

<h:formid=responseForm>

<h:graphicImageid=successImg

url=images/formsuccessjpgalt=注册成功!/>

<h>

<h:outputTextid=result

value=#{InfoBeanresponse}/></h>

<h:commandButtonid=back

value=返回action=su/>

<p>

</h:form>

</f:view>

</html>

()failurejsp


<%@pagecontentType=text/html;charset=gbk%>

<%@tagliburi=prefix=h%>

<%@tagliburi=prefix=f%>

<html>

<head>

<title>

用户注册失败

</title>

</head>

<bodybgcolor=white>

<f:view>

<h:formid=responseForm>

<h:graphicImageid=successImg

url=images/formerrorjpgalt=注册失败!/>

<h>

<h:outputTextid=result

value=#{InfoBeanresponse}/></h>

<h:commandButtonid=back

value=返回action=su/>

<p>

</h:form>

</f:view>

</html>

()webxml

<?xmlversion=?>

<webappxmlns=

xmlns:xsi=instance

xsi:schemaLocation=

app__xsd

version=>

<displayname>example</displayname>

<contextparam>

<paramname>contextConfigLocation</paramname>

<paramvalue>/WEBINF/applicationContextxml</paramvalue>

</contextparam>

<listener>

<listenerclass>orgsprntextContextLoaderListener</listenerclass>

</listener>

<servlet>

<displayname>FacesServlet</displayname>

<servletname>FacesServlet</servletname>

<servletclass>javaxfaceswebappFacesServlet</servletclass>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>FacesServlet</servletname>

<urlpattern>/example/*</urlpattern>

</servletmapping>

</webapp>

()applicationContextxml

<?xmlversion=encoding=UTF?>

<!DOCTYPEbeansPUBLIC//SPRING//DTDBEAN//EN

beansdtd>

<beans>

<beanid=dataSource

class=orgspringframeworkjndiJndiObjectFactoryBean>

<propertyname=jndiName>

<value>java:/MySqlDS</value>

</property>

</bean>

<beanid=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<propertyname=dataSource>

<reflocal=dataSource/>

</property>

<propertyname=mappingResources>

<list>

<value>

com/openv/spring/service/hibernate/UserInfohbmxml

</value>

</list>

</property>

<propertyname=hibernateProperties>

<props>

<propkey=hibernatedialect>

netsfhibernatedialectMySQLDialect

</prop>

<propkey=hibernateshow_sql>

true

</prop>

</props>

</property>

</bean>

<beanid=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<propertyname=sessionFactory>

<reflocal=sessionFactory/>

</property>

</bean>

<beanid=exampleServiceTarget

class=comopenvspringserviceimplExampleManagerImpl>

<propertyname=userinfo>

<reflocal=userinfoDAO/>

</property>

</bean>

<beanid=exampleService

class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>

<propertyname=transactionManager>

<reflocal=transactionManager/>

</property>

<propertyname=target>

<reflocal=exampleServiceTarget/>

</property>

<propertyname=transactionAttributes>

<props>

<propkey=get*>

PROPAGATION_REQUIREDreadOnly

</prop>

<propkey=set*>

PROPAGATION_REQUIRED

</prop>

</props>

</property>

</bean>

<beanid=userinfoDAO

class=comopenvspringservicedaoimplUserInfoDAO>

<propertyname=sessionFactory>

<reflocal=sessionFactory/>

</property>

</bean>

</beans>

()facesconfigxml


<?xmlversion=encoding=UTF?>

<!DOCTYPEfacesconfigPUBLIC

//SunMicrosystemsInc//DTDJavaServerFacesConfig//EN

facesconfig__dtd>

<facesconfig>

<application>

<localeconfig>

<defaultlocale>zh_CN</defaultlocale>

</localeconfig>

</application>

<navigationrule>

<description>

JSFHomePage

</description>

<fromviewid>/homejsp</fromviewid>

<navigationcase>

<description>

success

</description>

<fromoutcome>success</fromoutcome>

<toviewid>/successjsp</toviewid>

</navigationcase>

<navigationcase>

<description>

failure

</description>

<fromoutcome>failure</fromoutcome>

<toviewid>/failurejsp</toviewid>

</navigationcase>

</navigationrule>

<navigationrule>

<description>

</description>

<fromviewid>/successjsp</fromviewid>

<navigationcase>

<description>

</description>

<fromoutcome>su</fromoutcome>

<toviewid>/homejsp</toviewid>

</navigationcase>

</navigationrule>

<navigationrule>

<description>

</description>

<fromviewid>/failurejsp</fromviewid>

<navigationcase>

<description>

</description>

<fromoutcome>su</fromoutcome>

<toviewid>/homejsp</toviewid>

</navigationcase>

</navigationrule>

<managedbean>

<description>

InfoBean

</description>

<managedbeanname>InfoBean</managedbeanname>

<managedbeanclass>

comopenvspringjsfInfoBean

</managedbeanclass>

<managedbeanscope>session</managedbeanscope>

<managedproperty>

<propertyname>minimum</propertyname>

<propertyclass>long</propertyclass>

<value></value>

</managedproperty>

<managedproperty>

<propertyname>maximum</propertyname>

<propertyclass>long</propertyclass>

<value></value>

</managedproperty>

</managedbean>

</facesconfig>

上一篇:Composite模式及其在JSF中的应用

下一篇:struts标签+jstl标签之国际化实例