2017-02-22 5 views
2

Я новичок в Struts2. Это всего лишь пример моей проблемы. У меня есть два раскрывающихся списка на моей странице jsp с именем Author и Books.Когда я выбираю Author, другой раскрывающийся список должен заполнять список книг, относящийся к этому автору.Как передать список из класса действия Struts в Jsp dropdown

Что я здесь сделал, я выбрал идентификатор автора и передал его методу класса действий с помощью jQuery ajax. Используя метод dao class, я получил список имен книг, относящихся к этому id. Все в порядке.

Проблема в том, как передать этот список из класса действия в раскрывающийся список книг в jsp?

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

Это мое действие класс

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? –

Смежные вопросы