如何将列表从Struts动作类传递给Jsp下拉列表

时间:2017-02-22 04:14:52

标签: javascript java jquery jsp struts2

我是 Struts2 的新手。这只是我的问题的一个例子。我在我的jsp页面中有两个下拉列表,名为Author和Books。当我选择Author时,其他下拉列表应该填充与该作者相关的书籍列表。

我在这里做了什么,我选择了作者id并使用jQuery ajax将其传递给action类方法。使用dao class方法我得到了与该id相关的书名列表。一切都很好。

问题是如何将该列表从动作类传递到jsp中的Book下拉列表?

function getBooks(){

var authorId = document.getElementById("authorId").value;

	 $.ajax({ 
	    method: "GET",
	    url: "getBooks",
	    data: { "authorId" : authorId},
	    traditional: true,
	    success:
	        function()
	        {
	    	console.log("Success");
	        },
	    error: 
		   function()
	        {
	    	console.log("Fail");
	        }
	});
}

这是我的Action类

public class ProjectPostAction {
	private int authorId;

	public final int getAuthorId() {
		return authorId;
	}

	public final void setAuthorId(int authorId) {
		this.authorId = authorId;
	}

public String getBooks() throws DAOTransientException, DBConfigException{
		
		BookDao bookDao = new BookDao();
		List <Book> bookList = bookDao.getBooks(this.authorId);
		
		for(int i=0; i<bookList.size(); i++){
			System.out.println("Book Names " + bookList.get(i).getName());
		}

		return "success";		
	}
......................

}

2 个答案:

答案 0 :(得分:1)

或许可以尝试重新观看?

<强>动作

String bookListByAuthorId = "<select name="bookListByAuthorId">";

for(int i=0; i<bookList.size(); i++){
            bookListByAuthorId += "<option value='" + i + "'>" + bookList.get(i).getName() + "</option>";
        }

bookListByAuthorId += "</select>";

return bookListByAuthorId;

<强> AJAX

    success:
        function(result)
        {
        $("body").append(result);
        },

答案 1 :(得分:1)

试试这个。

public class ProjectPostAction {
	private int authorId;
	private InputStream inputStream;

	public final InputStream getInputStream() {
		return inputStream;
	}

	public final void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}


	public final int getAuthorId() {
		return authorId;
	}

	public final void setAuthorId(int authorId) {
		this.authorId = authorId;
	}


public String getBooks() throws DAOTransientException, DBConfigException{
		
		BookDao bookDao = new BookDao();
		List <Book> bookList = bookDao.getBooks(this.authorId);
		
		String bookListByAuthorId = "<select name="bookListByAuthorId">";

		for(int i=0; i<bookList.size(); i++){
            bookListByAuthorId += "<option value='" + i + "'>" + bookList.get(i).getName() + "</option>";
        }

		bookListByAuthorId += "</select>";
		inputStream = new StringBufferInputStream(bookListByAuthorId);

		return "success";		
	}
......................

}

/////////////////////////////////////////////

<action name="getBooks" class="#myActionClass" method="getBooks">
			<result name="success" type="stream">
			<param name="contentType">text/html</param>
            <param name="inputName">inputStream</param>
        	</result>
			<result name="failure">./failure.jsp</result>
			<result name="error">./error.jsp</result>
		</action>