Ajax Struts2-如果调用Action-Bean2,如何从Action-Bean1调用方法?
我创建了一种称为ProfileSelector的操作,通过一些ajax调用(使用JQuery库)加载该操作 代码如下:Ajax Struts2-如果调用Action-Bean2,如何从Action-Bean1调用方法?,ajax,model-view-controller,jsp,struts2,javabeans,Ajax,Model View Controller,Jsp,Struts2,Javabeans,我创建了一种称为ProfileSelector的操作,通过一些ajax调用(使用JQuery库)加载该操作 代码如下: // BEAN public class ProfileSelector extends ActionSupport implements ParameterAware { private String profilePage; private Map<String, String[]> parameters; @Override
// BEAN
public class ProfileSelector extends ActionSupport implements ParameterAware {
private String profilePage;
private Map<String, String[]> parameters;
@Override
public String execute() throws Exception {
profilePage=getParameterValue("profilePage");
return profilePage;
}
public String getParameterValue(String param) {
if (getParameters().get(param)==null) return "main";
return ((String[])getParameters().get(param))[0];
}
public Map<String, String[]> getParameters() { return parameters; }
public void setParameters(Map<String, String[]> maps) { this.parameters=maps; }
public String getProfilePage() { return profilePage; }
public void setProfilePage(String profilePage) { this.profilePage=profilePage; }
}
// STRUTS.XML
<action name="profile" class="model.ProfileSelector" >
<result name="main">/profile/profile_main.jsp</result>
<result name="edit">/profile/profile_edit.jsp</result>
<result name="editConfirm">/profile/profile_edit.jsp</result>
<result name="pm">/profile/profile_pm.jsp</result>
<result name="articles">/profile/profile_articles.jsp</result>
</action>
// PAGE.JSP
<c:choose>
<c:when test="${profilePage=='edit'}">
<s:div>
EDIT
<s:url id="edit" action="profile.action"><s:param name="profilePage">editConfirm</s:param></s:url>
<sj:submit href="%{edit}" targets="profileContent" value="Edit" />
</s:div>
</c:when>
<c:when test="${profilePage=='editConfirm'}">
// HERE I NEED TO LOAD A VALUE FROM ANOTHER BEAN-ACTION, not the profile one
</c:when>
</c:choose>
//BEAN
公共类ProfileSelector扩展ActionSupport实现Parameterware{
私有字符串配置文件页;
私有映射参数;
@凌驾
公共字符串execute()引发异常{
profilePage=getParameterValue(“profilePage”);
返回配置文件页面;
}
公共字符串getParameterValue(字符串参数){
如果(getParameters().get(param)==null)返回“main”;
返回((字符串[])getParameters().get(参数))[0];
}
公共映射getParameters(){return parameters;}
public void setParameters(映射映射){this.parameters=maps;}
公共字符串getProfilePage(){return profilePage;}
public void setProfilePage(字符串profilePage){this.profilePage=profilePage;}
}
//STRUTS.XML
/profile/profile_main.jsp
/profile/profile_edit.jsp
/profile/profile_edit.jsp
/profile/profile_pm.jsp
/profile/profile_articles.jsp
//PAGE.JSP
编辑
编辑确认
//这里我需要从另一个BEAN-ACTION加载一个值,而不是从概要文件加载一个值
查看(代码中)这里我需要从另一个BEAN-ACTION加载一个值,而不是profile one:这里我需要从另一个ACTION BEAN加载(例如)一个方法。(示例
我该怎么做呢?我认为使用Struts是不可能的,因为我一次只能调用一个操作。那么,拦截器呢?我需要改变我的整个应用程序结构吗
让我知道。干杯有一些不正确的地方,因为你永远不想为任何事情执行两个不同的操作。没有办法。但是,你可以从jsp发出ajax调用,指向不同的操作并加载值。/BEAN
public class ListBookAction extends ActionSupport {
BookService bookService;
List<Book> bookList;
public List<Book> getPostedBooks(){
List<Book> bookList = new ArrayList<Book>();
bookList = bookService.getUserPostedBooks();
return bookList;
}
public String show(){
bookList = getPostedBooks();
return "list";
}
public List<Book> getBookList() {
return bookList;
}
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
}
附言:如果你熟悉Struts1,它的工作原理就像DispatchAction一样。我不理解Struts2:不是真正的MVC。它只是一个动作VC…:)是的,Struts2是MVC。我建议你选一本关于Struts2的好书,因为你有很多问题。据我所知,该模型的存在是为了将我的数据管理到服务器(例如,我在JSF上见过Bean作用域)。在struts2上,我需要手动管理数据(比如将对象放入会话)。因此,与其他过程语言没有太大区别。比如PHP。也许我错了:)(我希望如此……)你不能在一次呼叫中呼叫两个动作,这不是真的,但总的来说这不是一个好主意。它被称为动作链接(不推荐,通常被视为糟糕的设计),但当您调用第一个动作时,它会将其所有值加载到值堆栈中,然后调用下一个动作并将其所有值添加到值堆栈中。
<action name="*ListBook" class="com.example.ListBookAction" method="{1}">
<result name="list" type="tiles-defs">listbook.listbook</result>
</action>
http://localhost:8888/showListBook.action