Updating contents of a jsp page without refreshing

后端 未结 2 1440
南笙
南笙 2021-01-03 03:35

I have a jsp page which shows the contents of a table. While the user views a page the contents of the table changes on a second-by-second basis. So the user has to refresh

2条回答
  •  猫巷女王i
    2021-01-03 04:16

    You should look into using Ajax (jQuery is my preferred method).

    http://api.jquery.com/jQuery.get/

    http://api.jquery.com/jQuery.post/

    This would then hit a controller that would return the data you want without refreshing the page.

    So for instance, if you had a login.jsp...

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="true" %>
    
    
    
        Login
    
    
    

    Hello please login to this application

    Username : Password :

    In your controller, you would then hit the Model, but for simplicity, I've done a very simple example...

    /**
     * Handles requests for the application home page.
     */
    @Controller
    public class LoginController {
    
        private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    
        Util util;
    
        /**
         * Simply selects the home view to render by returning its name.
         */
        @RequestMapping(value = "/login", method = RequestMethod.POST)
        public String home(Locale locale, Model model, String username, String password) {
    
    
            if(username.equalsIgnoreCase("david"))
            {
                model.addAttribute("validUser", "Welcome " + username );
    
                return "home";
            }
            else
            {
                model.addAttribute("validUser", "Incorrect username and password");
                return "home";
            }
    
        }
    
    }
    

    This would then add a slow scrolling bit of html to the div to say if it's valid, the code for home is below...

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="true" %>
    
    
    

    ${validUser}.

提交回复
热议问题