2017-02-22 46 views
2

我是Struts2的新成員。這只是我的問題的一個例子。在我的jsp頁面中有兩個名爲Author和Books的下拉列表。當我選擇Author時,其他下拉列表應該填充與該作者相關的書籍列表。如何將列表從Struts動作類傳遞給Jsp下拉列表

我在這裏做了什麼,我得到了選定的作者id,並將它傳遞給使用jQuery ajax的action類方法。使用dao類methos我得到了與該id相關的書名列表。每件事情都很好。

問題是如何將該列表從動作類傳遞給jsp中的Book下拉?

function getBooks(){ 
 

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

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

這是我的Action類

public class ProjectPostAction { 
 
\t private int authorId; 
 

 
\t public final int getAuthorId() { 
 
\t \t return authorId; 
 
\t } 
 

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

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

 
\t \t return "success"; \t \t 
 
\t } 
 
...................... 
 

 
}

+0

希望這refrance指南您https://coderanch.com/t/585636/framework/Dependable-dropdowns-jsp-struts –

+0

嘗試也http://www.raistudies.com/struts-1/ajax-with-struts-example/ –

回答

1

試試這個。

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

 
\t public final InputStream getInputStream() { 
 
\t \t return inputStream; 
 
\t } 
 

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

 

 
\t public final int getAuthorId() { 
 
\t \t return authorId; 
 
\t } 
 

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

 

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

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

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

 
\t \t return "success"; \t \t 
 
\t } 
 
...................... 
 

 
} 
 

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

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

1

也許可以嘗試retrun查看?

行動

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

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

bookListByAuthorId += "</select>"; 

回報bookListByAuthorId;

AJAX

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

如何配置struts.xml? –