Struts2 - 我可以这样做还是框架限制?

发布于 2024-10-06 14:00:08 字数 1599 浏览 3 评论 0原文

我想做的事情很简单:

我有一个加载的菜单,涉及某些变量的值。让我向我们展示代码:

<s:div cssClass="menu_table" id="loginContent">
    <c:choose>
        <c:when test="${not action.loggedOn}">
            <s:form action="checkLogin" theme="simple">
                <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>

行为非常简单:如果 action.loggedOn 为 true,我将加载一部分代码,否则加载另一部分。

现在,当我加载每个或另一个时,我可以对服务器进行 AJAX 调用(通过使用 JQuery 支持),从而更改错误变量的值(是字符串)和loggingOn的值(布尔值) 。

我想知道(通过使用这个框架)是否可以在服务器上直接执行这部分代码并在客户端上重新加载? (用更少的话说,我的意思是:是否可以执行jsp页面服务器端(对于标准请求,但这次使用ajax)并仅向客户端呈现一小部分代码)

或者(正如我认为的)我需要由于ajax调用后的字符串结果,创建客户端函数并重新绘制我的div(例如,使用JSON变量或其他Jquery函数)?

使用 JSF 和其他框架,第一个选项是可能的。我也可以用 Struts 做到这一点吗?希望我的问题很清楚:)

只是好奇。

谢谢

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">
                <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2024-10-13 14:00:08

我认为最干净的方法是在控制器中创建一种新方法,该方法仅返回菜单部分的 jsp(如您在问题中显示的那样)。您可以进行 ajax 调用,然后在菜单容器中加载新的 jsp:

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

JSP 始终在服务器中执行,而 JavaScript 在客户端中执行。但是您可以在控制器中创建多种方法,这些方法只能渲染整个页面的一部分,并使用它们只更新一部分。

编辑:如何在同一控制器方法中发送 json 或 html

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

并在您的操作中发送类似这样的内容

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

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";
    }
}
不爱素颜 2024-10-13 14:00:08

据我所知,您所问的问题是不可能的,您必须选择您在帖子中建议的重新绘制选项

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文