如何将列表从 Struts 操作 class 传递到 Jsp 下拉列表

How to pass list from Struts action class to Jsp dropdown

我是 Struts2 的新手。这只是我的 problem.I 的一个示例,在我的 jsp 页面中有两个名为 Author 和 Books.When I select Author 的下拉列表,其他下拉列表应填充与该作者相关的书籍列表.

我在这里所做的是,我获得了 selected 作者 ID 并使用 jQuery ajax 将其传递给操作 class 方法。使用 dao class 方法,我得到了与该 id 相关的书名列表。一切都很好。

问题是如何将该列表从操作 class 传递到 jsp 中的图书下拉列表?

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");
         }
 });
}

这是我的行动class

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";  
 }
......................

}

也许可以尝试返回查看?

操作

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 bookList AuthorId;

AJAX

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

试试这个。

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>