从struts 2中的sql <sj:select>中获取值的问题</sj:select>

时间:2011-06-27 09:26:09

标签: hibernate struts2

我正在尝试在保存在MySQL数据库中的选择框中获取值。这是我的JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sj" uri="/struts-jquery-tags" %>
<%@taglib prefix="sjg" uri="/struts-jquery-grid-tags" %>
<sj:head/>
<s:form theme="simple"  id="forvie">
<table>
    <tr>
        <td>
            <s:label value="Name"/></td>
        <td>
            <s:url id="mm" action="myact" namespace="/this"/>
            <sj:select id="iddepartmant"
                       name="iddepartmant"
                       list="listname"
                       listKey="iddepartmant"
                       listValue="deptname"
                       href="%{mm}" /></td>

        <td><s:textfield id="iddepartmant" name="iddepartmant"/></td>

    </tr>
</table>
</s:form>

我制作了2个Java类。一个是action

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import dao.Departmant;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import service.saveservice;
import utils.HibernateUtil;

/**
 *
 * @author User014
 */
public class Dropdown extends ActionSupport {

private List listname;
private String firstname;
public String showit() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    listname = new ArrayList();
    try {
        session.beginTransaction();
        saveservice sasa1 = new saveservice();
        Iterator itr =sasa1.getsome().iterator();
        while(itr.hasNext()){
            Departmant dptt= (Departmant) itr.next();
            if (dptt != null) {
                listname.add(new Cons(
                        dptt.getIddepartmant(),
                        dptt.getDeptname()));
            }
        }

     session.getTransaction().rollback();
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }

    return SUCCESS;
}

public List getListname() {
    return listname;
}

public void setListname(List listname) {
    this.listname = listname;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

}

第二个是service。在那里我定义了一个方法:

public List getsome() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    List list1 = new ArrayList();
    try {
        Query qry = session.createQuery("from Departmant");
        list1 = qry.list();

    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }
    return list1;
}

我在列表中返回了一个值,但它没有出现在JSP中。为什么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为<sj:select>标记需要JSON结果。见http://code.google.com/p/struts2-jquery/wiki/SelectTag