Java中刷新页面的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中刷新页面的问题相关的知识,希望对你有一定的参考价值。

初学!要详讲!关于Java编程的题。我的疑问是在java中关于页面刷新的问题1。我的要求是用按键控制页面刷新(在原有显示屏上,显示变更的数据),并计刷新次数。2。自动刷新,并计次数。请讲解全套function.

参考技术A 上面的一车代码全部不用看,只用看<body> <% int count=0; if(application.getAttribute("count")==null) count=1; else count=Integer.parseInt((String)application.getAttribute("count")); count+=1; out.print("第"+count+"次"); application.setAttribute("count",String.valueOf(count)); %> </body> 这段就行了,她就是设置一个全应用程序的变量,也就是application.getAttribute("count")当第1 次进来的时候就是null,所以count=1; ,第2次进来的时候就有值了,然后取出里面的值然后+1然后在存到全局变量中。也就是application.setAttribute("count",String.valueOf(count)); 不知道你明白没 参考技术B <%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'A.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta content="2" http-equiv="refresh"> <!-- 页面自动刷新的代码:每隔2s刷新一次 --> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head> <body> <% int count=0; if(application.getAttribute("count")==null) count=1; else count=Integer.parseInt((String)application.getAttribute("count")); count+=1; out.print("第"+count+"次"); application.setAttribute("count",String.valueOf(count)); %> </body></html>

提交表单时,页面刷新,检票口中的 PageParameters 为空

【中文标题】提交表单时,页面刷新,检票口中的 PageParameters 为空【英文标题】:Onsubmit of form, page get refreshed and PageParameters gets empty in wicket 【发布时间】:2020-02-16 07:46:02 【问题描述】:

在 LoginPage.java 中,我添加了一些 PageParameters 并将用户重定向到另一个页面。

PageParameters parameters = new PageParameters();
parameters.add("firstName", firstNameValue);
parameters.add("lastName", lastNameValue);
parameters.add("emailAddress", emailAddressValue);

throw new RedirectToPageException(UserInformationPage.class, parameters);

在 AdditionalInformationPage.java 中

public class UserInformationPage extends BaseWebPage 
    private static final long serialVersionUID = 3614392599102678526L;

    public UserInformationPage(final PageParameters parameters) 
        super(parameters);
        setOutputMarkupId(true);

        String firstName, lastName, emailAddress;

        firstName = parameters.get("firstName").toOptionalString();
        lastName = parameters.get("lastName").toOptionalString();
        emailAddress = parameters.get("emailAddress").toOptionalString();

        WebMarkupContainer userInformationPageWrapper = new WebMarkupContainer("userInformationPageWrapper");
        userInformationPageWrapper.add(new UserInformationPanel("userInformationPageContent", firstName, lastName, emailAddress));
        add(userInformationPageWrapper.setMarkupId("userInformationPageWrapper"));
    

用户信息面板.java

public class UserInformationPanel extends Panel 
    private static final long serialVersionUID = -1016518626600751985L;

    public UserInformationPanel(String id, String idpUuid, firstName, lastName, emailAddress) 
        super(id);
        setOutputMarkupId(true);

        Form<Void> userInformationForm = new CSRFSafeForm<Void>("userInformationForm") 
            private static final long serialVersionUID = 2633350725131958527L;

            @Override
            protected void onConfigure() 
                super.onConfigure();
                setVisible(true);
            
        ;

        FeedbackPanel errorFeedbackPanel = new TrackedFeedbackPanel("errorFeedback", new ErrorLevelFeedbackMessageFilter(FeedbackMessage.ERROR));
        errorFeedbackPanel.setMaxMessages(MAX_ERROR_MESSAGES);
        userInformationForm.add(errorFeedbackPanel.setOutputMarkupId(true));

        TextField<String> firstName = new TextField<>("firstName", firstName);
        firstName.add(StringValidator.maximumLength(DatabaseConstants.User.FIRST_NAME_MAX_LENGTH));
        userInformationForm.add(firstName.setRequired(true).setEnabled(true));

        TextField<String> lastName = new TextField<>("lastName", lastName));
        lastName.add(StringValidator.maximumLength(DatabaseConstants.User.LAST_NAME_MAX_LENGTH));
        userInformationForm.add(lastName.setRequired(true).setEnabled(true));

        EmailAddressValidator emailAddressValidator = EmailAddressValidator.getInstance();
        TextField<String> emailAddress = new EmailTextField("emailAddress", emailAddress), emailAddressValidator);
        emailAddress.setRequired(false)
                .add(UniqueEmailValidator.getInstance(UniqueEmailValidator.ErrorMsgType.REGISTER))
                .add(StringValidator.maximumLength(DatabaseConstants.EMAIL_ADDRESS_MAX_LENGTH));
        emailAddress.setEnabled(false);
        userInformationForm.add(emailAddress);

        userInformationForm.add(new AjaxButton("submitButton") 
            private static final long serialVersionUID = -1723378347103997463L;

            @Override
            public void onSubmit(AjaxRequestTarget target, Form<?> form) 
                super.onSubmit(target, form);
                Map<String, Object> userAttributes = new LinkedHashMap<>();
                userAttributes.put("email_Address", emailAddress);
                userAttributes.put("first_Name", firstName);
                userAttributes.put("last_Name", lastName);
                // logic to save userAttributes in DB.
                throw new RedirectToUrlException("/home");
            

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) 
                super.onError(target, form);
                target.add(errorFeedbackPanel);
            
        );
        userInformationForm.setOutputMarkupId(true);
        add(userInformationForm);
    


html 文件中的按钮如下:

<button wicket:id="submitButton" type="submit" class="adb-button__primary nextStep">
                    <span><wicket:message key="Submit"/></span>
                </button>

第一次页面被成功渲染。字段已预先填充。当我第一次单击提交按钮时,页面被刷新并且页面参数为空。表单再次呈现,但值为空。在第二次单击时,它可以正常工作。

那么,如何停止页面刷新,以使 pageParams 不会为空,并且在第一次单击提交按钮时,它会验证表单并在页面上显示错误(如果有)?

【问题讨论】:

您好,您能解释一下变量 userAttributes 的用途吗?我看到您在 submitButton#onSubmit 中定义并初始化它,但它似乎没有在其他任何地方使用。 在重定向到主页之前,我必须保存 userAttributes。我已经更新了代码。 如果您使用标准表单而不是 CSRFSafeForm 是否有效? CRSFSafeForm 内部扩展表单。 【参考方案1】:

我无法添加评论,所以我建议这个答案。

请在onSubmit() 方法中检查这一行:throw new RedirectToUrlException("/home");。你为什么用它?这意味着每当用户提交表单时,您都会重定向到 /home,并且可能在家中重定向回 LoginPage。请检查 /home 路线发生的情况,或发布更多信息,以便我可以更好地帮助您。

【讨论】:

以上是关于Java中刷新页面的问题的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp调用页面后刷新没效果

刷新 JSP 变量而不重新加载页面

页面自动刷新程序示例

JAVA-JSP内置对象之response对象实现页面自动刷新

实现网页不刷新页面的情况下动态刷新表格数据

HTML 页面刷新问题