Struts2 action class when loading page

依然范特西╮ 提交于 2019-12-11 13:16:01

问题


I must be missing something that I hope someone can set me straight. The webapp I inherited uses struts2 and JSP pages, and I want to organize it using this layout manager ( http://igniteui.com/layout-manager/border-layout-markup ). There will be a menu on the left which has links that will load content into the center, while the 'main' page (header, footer, left menu) stays put which I believe fits the SPA principles. The 'content' in the middle will be JSP pages that have forms with input fields that are submitted after user actions.

What I'm not understanding is how struts actions fit into this. The app has actions that are defined in struts.xml and that works fine when that page/form is submitted. But what about when the new JSP is loaded into the content section of the main page? How or what action is associated with that loading?

Currently the main page uses an iframe for the content :

<div class="center">
    <iframe id="ifrmContent" name="iframe" src="jspDefaultContent.jsp" </iframe>
</div>

and when a link is clicked in the left menu this is how I switch content :

$("#ifrmContent").attr('src','jspNewContent.jsp');

First I should ask if that is the right/best way to do this, as I've seen/heard that using iframes is not ideal.

Second, the specific problem is that there is no 'action' associated with this new JSP content page loading. So the OGNL that was used to fill in some s:select element lists with data from the database does not work. Specifically this WILL work : "%{@com.my.class@countryList}" because its calling a static variable within that Java server class, but this does NOT : "%{getCities()}" as it is looking to call a method of an action class.. and there is none. It doesn't call the MAIN page action, nor does it call the content page action class.

How can I get an action class associated to the content page I'm loading from a menu link? Or, how can/should this be organized so I don't have these problems, given that I only want to refresh/submit the content and not the entire MAIN page?

Thanks for any advice.


回答1:


  1. There is nothing wrong using <iframe>s (for the right purposes), they're the best option for their usecases (eg. embed a document in a page). Go ahead and check if they're misused here, if you care.

  2. IgniteUI's Infragistics stuff does not seem to always suit Struts2 (nor Java at all) very well, they seem to be created with .NET ecosystem in mind. Another user asked about working with their File Uploader widget... be careful.

  3. When you set the src attribute of an <iframe>, that URL gets called. Since you are using Struts2, no JSP should be called directly, they should instead be the results of the actions executions.

    So... just call the actions, instead of the JSPs:

    <div class="center">
        <iframe id="ifrmContent" src="defaultContet.action" />
    </div>
    
    $("#ifrmContent").attr('src','newContent.action');
    



回答2:


First
You can use css to divide a page into header,footer,menu,body. This is a lightweight approach. This can replace iframe for your purpose.

  • iFrame will load the frame as a separate page. If you write a page with 1 iframe in the body area, its like loading 2 pages, which will make page loading a bit slower.
  • iframe content will be loaded first then the web page content will be rendered, thats why you will find some of the contents in some webpages are populated quickly while other content's are loading with delay.
  • You can use iFrame to load AD's but try to avoid it.

Second
How can I get an action class associated to the content page I'm loading from a menu link?

You can point action items of any of your JSP pages to the action servlet(struts.xml). You have to point an action from this JSP page to the action you wanted to map in Struts.xml.

It is the same model as the basic struts2 program. if you have a basic understanding of struts flow you can do this.

how can/should this be organized so I don't have these problems, given that I only want to refresh/submit the content and not the entire MAIN page?

If this is the case you shouldnot use iframes for content loading in body section. Iframe's will loading the entire webpage for each HTTP request.
Answer for this is to use AJAX with jQuery



来源:https://stackoverflow.com/questions/28521284/struts2-action-class-when-loading-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!