开发者

Struts2 - Can i do this or it's a framework limitation?

开发者 https://www.devze.com 2023-01-30 08:04 出处:网络
What i would like to do its easy : I have a menu that is loaded regardings the values of some variables. Let me show us the code :

What i would like to do its easy :

I have a menu that is loaded regardings the values of some variables. Let me show us the code :

<s:div cssClass="menu_table" id="loginContent">
    <c:choose>
        <c:when test="${not action.loggedOn}">
            <s:form action="checkLogin" theme="simple">
                &开发者_JS百科lt;span id="err" class="menu_err">
                    <s:property value="error" />
                </span>
                <s:textfield name="nickname" />
                <s:password name="password" />
                <span class="menu_span">
                    <sj:submit targets="loginContent" value="Login" />
                </span>
            </s:form>
        </c:when>
        <c:when test="${action.loggedOn}">
            <s:form action="checkLogout" theme="simple">
                <s:label cssClass="menu_title" value="Ciao #{nickname}" />
                <span class="menu_span">
                    <sj:submit targets="loginContent" value="Logout" />
                </span>
            </s:form>
         </c:when>
    </c:choose>
</s:div>

The behaviour is very easy : if the action.loggedOn is true, i load a part of code, else another one.

Now, when i load each or the other, i can do an AJAX call (by using JQuery support) to the server, that change the value of the error variable (is a string) and the value of the loggedOn (which is boolean).

What i'd like to know (by using this frameworks) is if its possible execute directly this part of code on the server and reload this on client? (In less words i mean : is it possible to execute the jsp page server side (as for a standard request, but this time with ajax) and render only a small part of code to the client)

Or (as i think) i need to create the client-side function and repaint my divs (with JSON variables, for example, or others Jquery functions) due to the string-result after the ajax call?

Using JSF and other frameworks the first option is possible. Can i also do this with Struts? Hope my question is clear :)

Just curios.

Thanks


I think the cleanest way would be to create a new method in the controller which just returns a jsp only of the part of the menu (as the one you show in the question). You can make the ajax call and then load in the container of your menu the new jsp:

$.ajax({ 
    url: 'yourUrl',  
    success: function(html){ 
        $('#menuContainer').html(html); }, 
    error: function() { 
        alert('There was an error!'); } 
    });

JSP is always executed in the server while JavaScript is executed in the client. But you can create several methods in the controller which can render only parts of the whole page and use them to update only one part.

Edit: How to send json or html inthe same controller method

<action name="HelloWorld" class="vaannila.HelloWorld">
    <result name="sendJson" type="json"/>
    <result name="sendHTML">/url/yourjsp.jsp</result>
</action>

and in your action something like this

public String execute(){
    if(json){
        return "sendJson";
    }
    if(html){
        return "sendHTML";
    }
}


As per my knowledge what you asking is not possible you have to go for repaint option what you have suggested in your post

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号