How To Implement a progress bar using Spring 3 MVC?

后端 未结 2 1001
灰色年华
灰色年华 2021-02-20 08:57

Can anyone teach me or direct to a working example to satisfy this requirement.

Scenario:

  1. List item My Web App is using spring mvc.
  2. One of the ser
2条回答
  •  花落未央
    2021-02-20 09:28

    Here is a possible solution to this progress bar problem:

    task.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    
    
        
    
            
    
            
    
        
        
    
          

    Progress Bar Example

    0%

    TaskController.java

    @Controller
    @RequestMapping(value = "/task")
    public class TaskController {
    
        private Task task;
    
        @RequestMapping("")
        protected ModelAndView page() {
            ModelAndView model = new ModelAndView(VIEW_DIR + "task");
    
            if (this.task == null) {
                this.task = new Task();
            }
    
            model.addObject("task", this.task);
    
            return model;
        }
    
        @RequestMapping(value = "/status", method = GET)
        public @ResponseBody
        String getStatus() {
    
            return task.getStatus();
        }
    
        @RequestMapping(value = "/progress", method = GET)
        public @ResponseBody
        int getProgress() {
    
            return task.getProgress();
        }
    
        public ModelAndView form(@ModelAttribute Task task) {
    
            this.task = task;
    
            ModelAndView model = new ModelAndView(VIEW_DIR + "task");
    
            task.execute();
    
            model.addObject("task", this.task);
    
            return model;
        }
    
    }
    

    Task.java

    public class Task {
    
        private int total;
        private int progress;
        private String status;
    
        public Task() {
            this.status = "created";
    
            // TODO get total here or pass via form
        }
    
        public void execute() {
    
            status = "executing";
    
            int i = 0;
    
            while (i < total && status.equals("executing")) {
    
                progress = (100 * (i + 1) / total);
    
                i++;
            }
        }
    
        public int getTotal() {
            return total;
        }
    
        public void setTotal(int total) {
            this.total = total;
        }
    
        public int getProgress() {
            return progress;
        }
    
        public void setProgress(int progress) {
            this.progress = progress;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    

提交回复
热议问题