Anton Romanov
Anton Romanov

Reputation: 385

JSP: How to update table by button pressing

H!

I have a simple app (Java + SpringBoot + JSP). Thats my Controller:

@Controller
public class MainController {

   private static List<Rss.Channel.Item> items = new LinkedList<>();

    static {
        for(Rss.Channel.Item its : test()){
            items.add(its);
        }
    }

I use the follow method to fill my "Personlist" jsp page:

@RequestMapping(value = { "/personList" }, method = RequestMethod.GET)
    public String viewPersonList(Model model) {
        model.addAttribute("persons", items);
        return "personList";
    }

test() - it doesn't matter. Some method...

JSP page "Personlist" is quite easy:

<div>
    <table border="1">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
        <c:forEach  items="${persons}" var ="person">
            <tr>
                <td>${person.title}</td>
            </tr>
        </c:forEach>
    </table>
</div>

So, How to add button "Refresh" to my JSP page to update table content, without page reloading.

Thanks!

Upvotes: 1

Views: 395

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148890

You cannot. At least not that way. The problem here is that a JSP is in fact a true servlet, that means that it is executed server side and just produces a flow of characters, ordinarily a HTML page. And the browser just receives that page and has no idea that it has been produced through a JSP.

Of course it is possible to only update a page without fully reloading it, the SO site is full of that. But it involves client side Javascript that can:

  • send other requests to the server, and normally receive Json formatted data (other formats are of course possible)
  • use that data to modify the DOM of the currently displayed page.

You can use plain Javascript to achieve that, but it is more common to use higher level frameworks like jQuery that will hide the specific behaviour of specific browsers and provide you with higher level components.

Upvotes: 2

Related Questions