浏览 1570 次
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-13
代码如下: jsp页面 <html:form action="/insertCity" method="post"> 城市ID:<html:text property="citypk"/><html:errors property="citypk"/><br/> 城市名称 : <html:text property="name"/><html:errors property="name"/><br/> 简码 : <html:text property="shortcode"/><html:errors property="shortcode"/><br/> 编码 : <html:text property="code"/><html:errors property="code"/><br/> <html:hidden property="status" value="insert"/> <html:hidden property="type" value="1"/> <html:submit value="添加"/><html:reset value="重置"/> </html:form> action public class InsertCityAction extends DispatchAction { private ICityDAO icitydao; public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { InsertCityForm insertCityForm = (InsertCityForm) form; String code=insertCityForm.getCode(); String shortcode=insertCityForm.getShortcode(); TCity tcity=null; try { tcity=new TCity(); tcity.setCityPk(insertCityForm.getCitypk()); tcity.setName(insertCityForm.getName()); tcity.setCode(insertCityForm.getCode()); tcity.setShortcode(insertCityForm.getShortcode()); tcity.setDr(0); this.icitydao.insert(tcity); return mapping.findForward("insertcitysuccess"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("insertcityflag", "true"); return mapping.getInputForward(); } } public ICityDAO getIcitydao() { return icitydao; } public void setIcitydao(ICityDAO icitydao) { this.icitydao = icitydao; } } DAO部分 public interface ICityDAO { public boolean insert(TCity tcity) throws Exception; } DAOImpl部分: public class ICityDAOImpl extends HibernateDaoSupport implements ICityDAO { @Override public boolean insert(TCity tcity) throws Exception { boolean flag=false; try{ super.getSession().save(tcity); super.getSession().flush(); flag=true; }catch(Exception e){ e.printStackTrace(); } System.out.println(flag); return flag; } } struts-config.xml配置 <action attribute="insertCityForm" input="/insertCity.jsp" name="insertCityForm" parameter="status" path="/insertCity" scope="request" type="com.luluto.struts.action.InsertCityAction"> <forward name="insertcityfailure" path="/insertcityfailure.jsp" /> <forward name="insertcitysuccess" path="/insertcitysuccess.jsp" /> </action> applicationContext.xml的配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.connection.autocommit">true</prop> <!-- 显示sql语句 --> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/luluto/pub/vo/TCity.hbm.xml</value> <value>com/luluto/pub/vo/TStation.hbm.xml</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="icitydao" class="com.luluto.pub.dao.ICityDAO" abstract="true"></bean> <bean id="icitydaoimpl" class="com.luluto.pub.impl.ICityDAOImpl" parent="icitydao"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <bean name="/insertCity" class="com.luluto.struts.action.InsertCityAction"> <property name="icitydao"> <ref bean="icitydaoimpl"/> </property> </bean> TCity.hbm.xml配置部分 <hibernate-mapping> <class name="com.luluto.pub.vo.TCity" table="T_CITY"> <id name="cityPk" type="java.lang.Integer"> <column name="CITY_PK" /> <generator class="assigned"></generator> </id> <property name="code" type="java.lang.String"> <column name="CODE" length="30" not-null="true" /> </property> <property name="name" type="java.lang.String"> <column name="NAME" length="30" not-null="true" /> </property> <property name="shortcode" type="java.lang.String"> <column name="SHORTCODE" length="10" not-null="true" /> </property> <property name="dr" type="java.lang.Integer"> <column name="DR" not-null="true" /> </property> <set name="TStations" inverse="true"> <key> <column name="CITY_PK" not-null="true" /> </key> <one-to-many class="com.luluto.pub.vo.TStation" /> </set> </class> </hibernate-mapping> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |