陈高杰

kingaragorn

常用链接

统计

最新评论

6、7、8、9----SSH综合实战(Struts+Spring+Hibernate)----我的智囊团(提问及回答功能(上)1、2、3、4)

测试数据

INSERT INTO item(itemname, itemcode) VALUES ('JAVA SE', 1);
INSERT INTO item(itemname, itemcode) VALUES ('JAVA EE', 2);

INSERT INTO subitem(subname,itemid, subcode) VALUES ('面向对象', 1, 1);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('文件编程', 1, 2);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('多线程', 1, 3);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('网络编程', 1, 4);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('AWT/SWING', 1, 5);

INSERT INTO subitem(subname,itemid, subcode) VALUES ('JSP/Servlet', 2, 1);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('RMI/RMI-IIOP', 2, 2);
INSERT INTO subitem(subname,itemid, subcode) VALUES ('EJBs', 2, 3);

Item.java

package org.lxh.myzngt.vo;

import java.util.Set;

public class Item {
    private int itemid;
    private String itemname;
    private int itemcode;
    private Set subitems;

    public int getItemid() {
        return itemid;
    }

    public void setItemid(int itemid) {
        this.itemid = itemid;
    }

    public String getItemname() {
        return itemname;
    }

    public void setItemname(String itemname) {
        this.itemname = itemname;
    }

    public int getItemcode() {
        return itemcode;
    }

    public void setItemcode(int itemcode) {
        this.itemcode = itemcode;
    }

    public Set getSubitems() {
        return subitems;
    }

    public void setSubitems(Set subitems) {
        this.subitems = subitems;
    }

}

Subitem.java

package org.lxh.myzngt.vo;

public class Subitem {
    private int subid;
    private String subname;
    private String itemid;
    private int subcode;
    private Item item;

    public int getSubid() {
        return subid;
    }

    public void setSubid(int subid) {
        this.subid = subid;
    }

    public String getSubname() {
        return subname;
    }

    public void setSubname(String subname) {
        this.subname = subname;
    }

    public String getItemid() {
        return itemid;
    }

    public void setItemid(String itemid) {
        this.itemid = itemid;
    }

    public int getSubcode() {
        return subcode;
    }

    public void setSubcode(int subcode) {
        this.subcode = subcode;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }
}

Item.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.lxh.myzngt.vo.Item" table="item">
        <id name="itemid" type="java.lang.Integer">
            <column name="itemid" />
            <generator class="native" />
        </id>
        <property name="itemname" type="java.lang.String">
            <column name="itemname" length="50" />
        </property>
        <property name="itemcode" type="java.lang.Integer">
            <column name="itemcode" />
        </property>
        <set name="subitems" inverse="true" cascade="all"
            table="subitem" lazy="true" order-by="subcode">
            <key>
                <column name="itemid" />
            </key>
            <one-to-many class="org.lxh.myzngt.vo.Subitem" />
        </set>

    </class>
</hibernate-mapping>

Subitem.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.lxh.myzngt.vo.Subitem" table="subitem">
        <id name="subid" type="java.lang.Integer">
            <column name="subid" />
            <generator class="native" />
        </id>
        <many-to-one name="item" class="org.lxh.myzngt.vo.Item">
            <column name="itemid" />
        </many-to-one>

        <property name="subname" type="java.lang.String">
            <column name="subname" length="50" />
        </property>
        <property name="subcode" type="java.lang.Integer">
            <column name="subcode" />
        </property>
    </class>
</hibernate-mapping>

把前面的org.lxh.zngt.struts.form改为org.lxh.myzngt.struts.form
             org.lxh.zngt.struts.action改为org.lxh.myzngt.struts.form

QuestionForm.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * MyEclipse Struts Creation date: 07-14-2008
 *
 * XDoclet definition:
 *
 * @struts.form name="questionForm"
 */
public class QuestionForm extends ActionForm {
    /*
     * Generated fields
     */

    /** qid property */
    private String qid;

    /** questiontime property */
    private String questiontime;

    /** commenflag property */
    private String commenflag;

    /** title property */
    private String title;

    /** content property */
    private String content;

    /** status property */
    private String status;

    /** grade property */
    private String grade;

    /** itemid property */
    private String itemid;

    /** subid property */
    private String subid;

    /** userid property */
    private String userid;

    /** acceptflag property */
    private String acceptflag;

    /** offerscore property */
    private String offerscore;

    /** clickcount property */
    private String clickcount;

    private String checkcode;

    private int type;

    // 1:表示进行增加操作
    // 2:表示查看问题

    /*
     * Generated Methods
     */

    public String getCheckcode() {
        return checkcode;
    }

    public void setCheckcode(String checkcode) {
        this.checkcode = checkcode;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    /**
     * Method validate
     *
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (type == 1) {
            if (this.title == null || "".equals(this.title)) {
                errors.add("title", new ActionMessage("question.title.null"));
            }
            if (this.content == null || "".equals(this.content)) {
                errors.add("content",
                        new ActionMessage("question.content.null"));
            }
            if (!(this.offerscore == null || "".equals(this.offerscore))) {
                if (!this.offerscore.matches("\\d*")) {
                    errors.add("offerscore", new ActionMessage(
                            "question.offerscore.error"));
                }
            }
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }
            if (this.offerscore == null || "".equals(this.offerscore)) {
                this.offerscore = "5";
            }
        }
        return errors;
    }

    /**
     * Method reset
     *
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // TODO Auto-generated method stub
    }

    /**
     * Returns the qid.
     *
     * @return String
     */
    public String getQid() {
        return qid;
    }

    /**
     * Set the qid.
     *
     * @param qid
     *            The qid to set
     */
    public void setQid(String qid) {
        this.qid = qid;
    }

    /**
     * Returns the questiontime.
     *
     * @return String
     */
    public String getQuestiontime() {
        return questiontime;
    }

    /**
     * Set the questiontime.
     *
     * @param questiontime
     *            The questiontime to set
     */
    public void setQuestiontime(String questiontime) {
        this.questiontime = questiontime;
    }

    /**
     * Returns the commenflag.
     *
     * @return String
     */
    public String getCommenflag() {
        return commenflag;
    }

    /**
     * Set the commenflag.
     *
     * @param commenflag
     *            The commenflag to set
     */
    public void setCommenflag(String commenflag) {
        this.commenflag = commenflag;
    }

    /**
     * Returns the title.
     *
     * @return String
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the title.
     *
     * @param title
     *            The title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Returns the content.
     *
     * @return String
     */
    public String getContent() {
        return content;
    }

    /**
     * Set the content.
     *
     * @param content
     *            The content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Returns the status.
     *
     * @return String
     */
    public String getStatus() {
        return status;
    }

    /**
     * Set the status.
     *
     * @param status
     *            The status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * Returns the grade.
     *
     * @return String
     */
    public String getGrade() {
        return grade;
    }

    /**
     * Set the grade.
     *
     * @param grade
     *            The grade to set
     */
    public void setGrade(String grade) {
        this.grade = grade;
    }

    /**
     * Returns the itemid.
     *
     * @return String
     */
    public String getItemid() {
        return itemid;
    }

    /**
     * Set the itemid.
     *
     * @param itemid
     *            The itemid to set
     */
    public void setItemid(String itemid) {
        this.itemid = itemid;
    }

    /**
     * Returns the subid.
     *
     * @return String
     */
    public String getSubid() {
        return subid;
    }

    /**
     * Set the subid.
     *
     * @param subid
     *            The subid to set
     */
    public void setSubid(String subid) {
        this.subid = subid;
    }

    /**
     * Returns the userid.
     *
     * @return String
     */
    public String getUserid() {
        return userid;
    }

    /**
     * Set the userid.
     *
     * @param userid
     *            The userid to set
     */
    public void setUserid(String userid) {
        this.userid = userid;
    }

    /**
     * Returns the acceptflag.
     *
     * @return String
     */
    public String getAcceptflag() {
        return acceptflag;
    }

    /**
     * Set the acceptflag.
     *
     * @param acceptflag
     *            The acceptflag to set
     */
    public void setAcceptflag(String acceptflag) {
        this.acceptflag = acceptflag;
    }

    /**
     * Returns the offerscore.
     *
     * @return String
     */
    public String getOfferscore() {
        return offerscore;
    }

    /**
     * Set the offerscore.
     *
     * @param offerscore
     *            The offerscore to set
     */
    public void setOfferscore(String offerscore) {
        this.offerscore = offerscore;
    }

    /**
     * Returns the clickcount.
     *
     * @return String
     */
    public String getClickcount() {
        return clickcount;
    }

    /**
     * Set the clickcount.
     *
     * @param clickcount
     *            The clickcount to set
     */
    public void setClickcount(String clickcount) {
        this.clickcount = clickcount;
    }
}

AnswerForm.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * MyEclipse Struts
 * Creation date: 07-14-2008
 *
 * XDoclet definition:
 * @struts.form name="answerForm"
 */
public class AnswerForm extends ActionForm {
    /*
     * Generated fields
     */

    /** qid property */
    private String qid;

    /** anstime property */
    private String anstime;

    /** userid property */
    private String userid;

    /** quesans property */
    private String quesans;

    /** aid property */
    private String aid;

    /** status property */
    private String status;

    /** grade property */
    private String grade;

    private String checkcode;
   
    private int type;
   
    // 1:表示增加回答
   
    /*
     * Generated Methods
     */

    /**
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (type == 1) {
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }
            if (this.quesans == null || "".equals(this.quesans)) {
                errors.add("quesans", new ActionMessage("answer.quesans.null"));
            }
            if (this.qid == null || "".equals(this.qid)) {
                errors.add("qid",new ActionMessage("answer.qid.null")) ;
            }           
        }
        return errors;
    }

    /**
     * Method reset
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // TODO Auto-generated method stub
    }

    /**
     * Returns the qid.
     * @return String
     */
    public String getQid() {
        return qid;
    }

    /**
     * Set the qid.
     * @param qid The qid to set
     */
    public void setQid(String qid) {
        this.qid = qid;
    }

    /**
     * Returns the anstime.
     * @return String
     */
    public String getAnstime() {
        return anstime;
    }

    /**
     * Set the anstime.
     * @param anstime The anstime to set
     */
    public void setAnstime(String anstime) {
        this.anstime = anstime;
    }

    /**
     * Returns the userid.
     * @return String
     */
    public String getUserid() {
        return userid;
    }

    /**
     * Set the userid.
     * @param userid The userid to set
     */
    public void setUserid(String userid) {
        this.userid = userid;
    }

    /**
     * Returns the quesans.
     * @return String
     */
    public String getQuesans() {
        return quesans;
    }

    /**
     * Set the quesans.
     * @param quesans The quesans to set
     */
    public void setQuesans(String quesans) {
        this.quesans = quesans;
    }

    /**
     * Returns the aid.
     * @return String
     */
    public String getAid() {
        return aid;
    }

    /**
     * Set the aid.
     * @param aid The aid to set
     */
    public void setAid(String aid) {
        this.aid = aid;
    }

    /**
     * Returns the status.
     * @return String
     */
    public String getStatus() {
        return status;
    }

    /**
     * Set the status.
     * @param status The status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * Returns the grade.
     * @return String
     */
    public String getGrade() {
        return grade;
    }

    /**
     * Set the grade.
     * @param grade The grade to set
     */
    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getCheckcode() {
        return checkcode;
    }

    public void setCheckcode(String checkcode) {
        this.checkcode = checkcode;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}

IItemDAO.java

package org.lxh.myzngt.dao;

import java.util.List;

public interface IItemDAO {
    // 得到全部的栏目
    public List queryAll() throws Exception;
}


ItemForm.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 07-14-2008
 *
 * XDoclet definition:
 * @struts.form name="itemForm"
 */
public class ItemForm extends ActionForm {
    /*
     * Generated fields
     */

    /** itemcode property */
    private String itemcode;

    /** itemname property */
    private String itemname;

    /** itemid property */
    private String itemid;

    private int type ;
    // 1:查询全部,都不验证
   
    /*
     * Generated Methods
     */

    /**
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Method reset
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // TODO Auto-generated method stub
    }

    /**
     * Returns the itemcode.
     * @return String
     */
    public String getItemcode() {
        return itemcode;
    }

    /**
     * Set the itemcode.
     * @param itemcode The itemcode to set
     */
    public void setItemcode(String itemcode) {
        this.itemcode = itemcode;
    }

    /**
     * Returns the itemname.
     * @return String
     */
    public String getItemname() {
        return itemname;
    }

    /**
     * Set the itemname.
     * @param itemname The itemname to set
     */
    public void setItemname(String itemname) {
        this.itemname = itemname;
    }

    /**
     * Returns the itemid.
     * @return String
     */
    public String getItemid() {
        return itemid;
    }

    /**
     * Set the itemid.
     * @param itemid The itemid to set
     */
    public void setItemid(String itemid) {
        this.itemid = itemid;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}


IItemDAOImpl.java

package org.lxh.myzngt.dao.impl;

import java.util.List;

import org.lxh.myzngt.dao.IItemDAO;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class IItemDAOImpl extends HibernateDaoSupport implements IItemDAO {

    public List queryAll() throws Exception {
        String hql = "FROM Item AS i";
        List all = super.getSession().createQuery(hql).list();
        return all;
    }
}

ItemAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.lxh.myzngt.dao.IItemDAO;

/**
 * MyEclipse Struts
 * Creation date: 07-14-2008
 *
 * XDoclet definition:
 * @struts.action path="/item" name="itemForm" input="/form/item.jsp" parameter="status" scope="request" validate="true"
 */
public class ItemAction extends DispatchAction {
    private IItemDAO iitemdao;
   
    /*
     * Generated Methods
     */

    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward selectall(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        List all = null;
        try {
            all = this.iitemdao.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("all", all);
        return mapping.findForward("question");
    }

    public IItemDAO getIitemdao() {
        return iitemdao;
    }

    public void setIitemdao(IItemDAO iitemdao) {
        this.iitemdao = iitemdao;
    }
}

applicationContext.xml

    <bean id="iitemdao" class="org.lxh.myzngt.dao.IItemDAO"
        abstract="true">
    </bean>
    <bean id="iitemdaoimpl" class="org.lxh.myzngt.dao.impl.IItemDAOImpl"
        parent="iitemdao">
        <property name="hibernateTemplate">
            <ref bean="hibernateTemplate" />
        </property>
    </bean>
    <bean name="/jsp/ques/item"
        class="org.lxh.myzngt.struts.action.ItemAction" singleton="true">
        <property name="iitemdao">
            <ref bean="iitemdaoimpl"/>
        </property>
    </bean>

struts-config.xml

    <action
      attribute="questionForm"
      input="/jsp/errors.jsp"
      name="questionForm"
      parameter="status"
      path="/jsp/ques/question"
      scope="request"
      type="org.lxh.myzngt.struts.action.QuestionAction">
     
    </action>
    <action
      attribute="answerForm"
      input="/jsp/errors.jsp"
      name="answerForm"
      parameter="status"
      path="/jsp/ques/answer"
      scope="request"
      type="org.lxh.myzngt.struts.action.AnswerAction">
     
    </action>
    <action
      attribute="itemForm"
      input="/jsp/errors.jsp"
      name="itemForm"
      parameter="status"
      path="/jsp/ques/item"
      scope="request"
      type="org.lxh.myzngt.struts.action.ItemAction">
      <forward name="question" path="/jsp/ques/question.jsp"></forward>
    </action>
    </action-mappings>

template.jsp

<%@ page contentType="text/html;charset=gbk"%>
<!-- 作者:董鸣楠-->
<table width="760" border="0" cellpadding="0" cellspacing="0">
    <tr align="center">
        <td width="345"></td>
        <td width="212">
            <a href="${param.url}jsp/index.do?status=list">
            <img src="${param.url}images/banner_logo.jpg" alt="智囊团" width="170" height="78" border="0"/></a>
        </td>
        <td width="345">
            <table width="345"  border="0" cellspacing="0" cellpadding="0">
                <tr valign="middle">
                    <td align="right">
                        <a href="ques/item.do?status=selectall&type=0">
                            <img src="${param.url}images/banner_ico05.jpg" alt="我要提问" border="0">
                        </a>
                    </td>
                    <td width="138">
                        <a href="login.jsp">
                            <img src="${param.url}images/banner_ico06.jpg" alt="我要登录" border="0">
                        </a>
                    </td>
                </tr>
            </table>
        </td>
</tr>
</table>
<hr>

下面讲解一下如何进行后台测试

必须要把datasource改掉,改为普通的数据库连接

applicationContext.xml

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>org.gjt.mm.mysql.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3307/mldn</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value></value>
        </property>
    </bean>
    <!-- 
        <bean id="dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/mldn"></property>
        </bean>
    -->


要把MySQL驱动程序拷到项目之中

TestItem.java

package org.lxh.myzngt.test;

import org.lxh.myzngt.dao.IItemDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestItem {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        IItemDAO id = (IItemDAO) ctx.getBean("iitemdaoimpl");
        System.out.println(id.queryAll());
    }
}

[org.lxh.myzngt.vo.Item@1dcc2a3, org.lxh.myzngt.vo.Item@943dc4]

再在后台做一个实验

ItemAction.java

    public ActionForward selectall(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        List all = null;
        try {
            all = this.iitemdao.queryAll();
            Iterator iter = all.iterator();
            while (iter.hasNext()) {
                Item item = (Item) iter.next();
                System.out.println(item.getItemname());
                Iterator it = item.getSubitems().iterator();
                while (it.hasNext()) {
                    Subitem si = (Subitem)it.next();
                    System.out.println(" |- " + si.getSubname());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("all", all);
        return mapping.findForward("question");
    }

JAVA SE

 |- 面向对象
 |- 文件编程
 |- 多线程
 |- 网络编程
 |- AWT/SWING
JAVA EE

 |- JSP/Servlet
 |- RMI/RMI-IIOP
 |- EJBs

下面配置log4j.xml

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=e:/znt.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

作者在vo中的Subitem.java中犯了一个错误,把private int subcode写成了private String subcode结果花费了很大的精力

question.jsp

<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<html:html lang="true">
<head>
<title>MLDN —— 我的智囊团</title>
<META NAME="Generator"
    CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李兴华">
<META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center><jsp:include flush="true" page="../../inc/template.jsp">
    <jsp:param name="url" value="../../" />
</jsp:include> <SCRIPT language="javascript">
function subchk()
{
    document.questionForm.content.value= window.eWebEditor1.getPureHtml();
}
</SCRIPT> <%
 int temp = 0;
 %> <logic:present name="all" scope="request">
    <Script language="javascript">
    var subitemlength;
    item_id= new Array();
    sub_id=new Array();
    sub_name=new Array();
   
    <logic:iterate id="item" scope="request" name="all" indexId="ind">
        <logic:iterate id="sub" name="item" property="subitems">
            item_id[<%=temp%>]=new Array("${item.itemid}");
            sub_id[<%=temp%>]=new Array("${sub.subid}");
            sub_name[<%=temp%>]=new Array("${sub.subname}");
            <%temp++ ;%>
        </logic:iterate>
    </logic:iterate>
subitemlength=29;//二级栏目数组的长度
//根据所选一级栏目动态改变所对应的二级栏目
function changSubitem(itemid,subid)
{
    var subtemp=0;//添加二级栏目时数组的下标
    var index=subid.length;
    //删除原来二级栏目的选项
    for (i=index-1;i>=0;i--)
      {
         subid.options[i]=null;
      }
    var itemindex=itemid.selectedIndex;//所选的一级栏目   
    //添加所选一级栏目所对应的二级栏目
    for (i=0;i<subitemlength;i++)
     {
        if (itemid.options[itemindex].value==item_id[i])
         {
            subid.options[subtemp]=new Option(sub_name[i],sub_id[i]);
            subtemp=subtemp+1;
         }
     }     
}
</script>
    <html:form action="jsp/ques/question.do" method="post" onsubmit="subchk()">
        标题:<html:text property="title"></html:text>
        <br>
        栏目:
        <select name="itemid"
            onchange="changSubitem(document.questionForm.itemid,document.questionForm.subid)">
            <logic:iterate id="item" name="all" scope="request">
                <option value=${item.itemid } selected>${item.itemname}</option>
            </logic:iterate>
        </select>
        <select name="subid">
        </select>
        <script language="javascript">
            document.questionForm.itemid[0].selected=true ;
            document.questionForm.itemid.onchange() ;
        </script>
        <br>
        内容:<br><textarea name="content" style="display:none"></textarea>
        <IFRAME ID="eWebEditor1" src="edit/editor.html" frameborder="0"
            scrolling="no" width="467" height="200"></IFRAME>
        <br>
        悬赏分数:<html:text property="offerscore"></html:text>
        <br>
        验证码:<html:text property="checkcode"></html:text>
        <img src="../image.jsp">
        <br>
        <input type="hidden" name="status" value="insert">
        <input type="hidden" name="type" value="1">
        <input type="hidden" name="userid" value="${userid}">
        <input type="hidden" name="grade" value="${grade}">
        <html:submit value="提问"></html:submit>
        <html:reset value="重置"></html:reset>
    </html:form>
</logic:present></center>
</body>
</html:html>


Question.java

package org.lxh.myzngt.vo;

import java.util.Date;
import java.util.Set;

public class Question {
    private int qid;
    private String title;
    private String content;
    private int itemid;
    private int subid;
    private String userid;
    private String grade;
    private int offerscore;
    private int status;
    private Date questiontime;
    private int clickcount;
    private int acceptflag;
    private int commenflag;

    private Set answers;

    public int getAcceptflag() {
        return acceptflag;
    }

    public void setAcceptflag(int acceptflag) {
        this.acceptflag = acceptflag;
    }

    public int getClickcount() {
        return clickcount;
    }

    public void setClickcount(int clickcount) {
        this.clickcount = clickcount;
    }

    public int getCommenflag() {
        return commenflag;
    }

    public void setCommenflag(int commenflag) {
        this.commenflag = commenflag;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getItemid() {
        return itemid;
    }

    public void setItemid(int itemid) {
        this.itemid = itemid;
    }

    public int getOfferscore() {
        return offerscore;
    }

    public void setOfferscore(int offerscore) {
        this.offerscore = offerscore;
    }

    public int getQid() {
        return qid;
    }

    public void setQid(int qid) {
        this.qid = qid;
    }

    public Date getQuestiontime() {
        return questiontime;
    }

    public void setQuestiontime(Date questiontime) {
        this.questiontime = questiontime;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getSubid() {
        return subid;
    }

    public void setSubid(int subid) {
        this.subid = subid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public Set getAnswers() {
        return answers;
    }

    public void setAnswers(Set answers) {
        this.answers = answers;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

Answer.java

package org.lxh.myzngt.vo;

import java.util.Date;

public class Answer {
    private int aid;
    private String quesans;
    private String userid;
    private String grade;
    private Date anstime;
    private int status;
    private int qid;

    private Question question;

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public Date getAnstime() {
        return anstime;
    }

    public void setAnstime(Date anstime) {
        this.anstime = anstime;
    }

    public int getQid() {
        return qid;
    }

    public void setQid(int qid) {
        this.qid = qid;
    }

    public String getQuesans() {
        return quesans;
    }

    public void setQuesans(String quesans) {
        this.quesans = quesans;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

Question.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.lxh.myzngt.vo.Question" table="question">
        <id name="qid" type="java.lang.Integer">
            <column name="qid" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" length="50" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" length="65535" />
        </property>
        <property name="itemid" type="java.lang.Integer">
            <column name="itemid" />
        </property>
        <property name="subid" type="java.lang.Integer">
            <column name="subid" />
        </property>
        <property name="userid" type="java.lang.String">
            <column name="userid" length="50" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="grade" length="50" />
        </property>
        <property name="offerscore" type="java.lang.Integer">
            <column name="offerscore" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="status" />
        </property>
        <property name="questiontime" type="java.util.Date">
            <column name="questiontime" length="19" />
        </property>
        <property name="clickcount" type="java.lang.Integer">
            <column name="clickcount" />
        </property>
        <property name="acceptflag" type="java.lang.Integer">
            <column name="acceptflag" />
        </property>
        <property name="commenflag" type="java.lang.Integer">
            <column name="commenflag" />
        </property>
        <set name="answers" inverse="true" table="answer">
            <key>
                <column name="qid" />
            </key>
            <one-to-many class="org.lxh.myzngt.vo.Answer" />
        </set>
    </class>
</hibernate-mapping>

Answer.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.lxh.myzngt.vo.Answer" table="answer">
        <id name="aid" type="java.lang.Integer">
            <column name="aid" />
            <generator class="native" />
        </id>
        <many-to-one name="question" class="org.lxh.myzngt.vo.Question"
            fetch="select">
            <column name="qid" />
        </many-to-one>
        <property name="quesans" type="java.lang.String">
            <column name="quesans" length="50" />
        </property>
        <property name="userid" type="java.lang.String">
            <column name="userid" length="50" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="grade" length="50" />
        </property>
        <property name="anstime" type="java.util.Date">
            <column name="anstime" length="19" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="status" />
        </property>
    </class>
</hibernate-mapping>


IQuestionDAO.java

package org.lxh.myzngt.dao;

import org.lxh.myzngt.vo.Question;

public interface IQuestionDAO {
    // 增加问题
    public void insert(Question question) throws Exception;
}


IQuestionDAOImpl.java

package org.lxh.myzngt.dao.impl;

import org.lxh.myzngt.dao.IQuestionDAO;
import org.lxh.myzngt.vo.Question;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class IQuestionDAOImpl extends HibernateDaoSupport implements
        IQuestionDAO {

    public void insert(Question question) throws Exception {
        super.getSession().save(question);
    }
}

applicationContext.xml

    <bean id="iquestiondao" class="org.lxh.myzngt.dao.IQuestionDAO"
        abstract="true">
    </bean>
    <bean id="iquestiondaoimpl"
        class="org.lxh.myzngt.dao.impl.IQuestionDAOImpl"
        parent="iquestiondao">
        <property name="hibernateTemplate">
            <ref bean="hibernateTemplate" />
        </property>        
    </bean>
    <bean name="/jsp/ques/question"
        class="org.lxh.myzngt.struts.action.QuestionAction">
        <property name="iquestiondao">
            <ref bean="iquestiondaoimpl" />
        </property>
    </bean>


QuestionAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.lxh.myzngt.dao.IQuestionDAO;
import org.lxh.myzngt.struts.form.QuestionForm;
import org.lxh.myzngt.vo.Question;

/**
 * MyEclipse Struts
 * Creation date: 07-14-2008
 *
 * XDoclet definition:
 * @struts.action path="/question" name="questionForm" input="/form/question.jsp" parameter="status" scope="request" validate="true"
 */
public class QuestionAction extends DispatchAction {
    private IQuestionDAO iquestiondao;
    /*
     * Generated Methods
     */

    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        QuestionForm questionForm = (QuestionForm) form;
        String ccode = (String) request.getSession().getAttribute("ccode");
        String checkcode = questionForm.getCheckcode();
        if (!(checkcode.equals(ccode))) {
            ActionMessages errors = new ActionMessages();
            errors.add("checkcode", new ActionMessage("checkcode.error"));
            super.saveErrors(request, errors);
            return mapping.getInputForward();
        }
        Question que = new Question();
        que.setTitle(questionForm.getTitle());
        que.setContent(questionForm.getContent());
        que.setItemid(Integer.parseInt(questionForm.getItemid()));
        que.setSubid(Integer.parseInt(questionForm.getSubid()));
        que.setGrade(org.lxh.myzngt.util.IntegralGrade.getInstance()
                .getGradeInfo(Integer.parseInt(questionForm.getGrade())));
        que.setUserid(questionForm.getUserid());
        que.setOfferscore(Integer.parseInt(questionForm.getOfferscore()));
        que.setQuestiontime(new Date());
        que.setStatus(1);
        que.setAcceptflag(2);
        que.setCommenflag(0);
        try {
            this.iquestiondao.insert(que);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapping.findForward("insertdo");
    }

    public IQuestionDAO getIquestiondao() {
        return iquestiondao;
    }

    public void setIquestiondao(IQuestionDAO iquestiondao) {
        this.iquestiondao = iquestiondao;
    }
}  


struts-config.xml

<forward name="insertdo" path="/jsp/ques/question_do.jsp"></forward>


question_do.jsp

<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
    <title>MLDN —— 我的智囊团</title>
    <META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
    <META NAME="Author" CONTENT="李兴华">
    <META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
    <META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="../../inc/template.jsp">
    <jsp:param name="url" value="../../"/>
</jsp:include>
提问成功
</center>
</body>
</html:html>

本节到此为止

posted on 2008-07-13 15:56 陈高杰 阅读(559) 评论(0)  编辑  收藏 所属分类: SSH我的智囊团


只有注册用户登录后才能发表评论。


网站导航: