Skip to content

Commit

Permalink
Remove unnecessary servlet response manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Oct 2, 2023
1 parent 0908942 commit fce19de
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponse;
import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -121,19 +122,20 @@ public String htmxInitFindForm() {
@GetMapping("/owners")
public String ownersList(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model,
HttpServletResponse response) {
return processFindForm(page, owner, result, model, response, "owners/findOwners", "owners/ownersList");
return processFindForm(page, owner, result, model, "owners/findOwners", "owners/ownersList");
}

@HxRequest
@GetMapping("/owners")
public String htmxOwnersList(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
public HtmxResponse htmxOwnersList(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
Model model, HttpServletResponse response) {
return processFindForm(page, owner, result, model, response, FRAGMENTS_OWNERS_FIND_FORM,
String view = processFindForm(page, owner, result, model, FRAGMENTS_OWNERS_FIND_FORM,
"fragments/owners :: list");
return new HtmxResponse().addTemplate(view).pushHistory("/owners/find?lastName=" + owner.getLastName());
}

public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
Model model, HttpServletResponse response, String emptyView, String listView) {
Model model, String emptyView, String listView) {
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
owner.setLastName(""); // empty string signifies broadest possible search
Expand All @@ -154,18 +156,16 @@ public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner
}

// multiple owners found
return addPaginationModel(owner.getLastName(), page, model, ownersResults, response, listView);
return addPaginationModel(owner.getLastName(), page, model, ownersResults, listView);
}

private String addPaginationModel(String lastName, int page, Model model, Page<Owner> paginated,
HttpServletResponse response, String listView) {
private String addPaginationModel(String lastName, int page, Model model, Page<Owner> paginated, String listView) {
model.addAttribute("listOwners", paginated);
List<Owner> listOwners = paginated.getContent();
model.addAttribute("currentPage", page);
model.addAttribute("totalPages", paginated.getTotalPages());
model.addAttribute("totalItems", paginated.getTotalElements());
model.addAttribute("listOwners", listOwners);
response.addHeader("HX-Push-Url", "/owners?lastName=" + lastName + "&page=" + page);
return listView;
}

Expand All @@ -182,9 +182,7 @@ public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model mo

@HxRequest
@GetMapping("/owners/{ownerId}/edit")
public String htmxInitUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model, HttpServletRequest request,
HttpServletResponse response) {
response.addHeader("HX-Push-Url", request.getServletPath());
public String htmxInitUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
return handleInitUpdateOwnerForm(ownerId, model, FRAGMENTS_OWNERS_EDIT);
}

Expand Down Expand Up @@ -229,8 +227,7 @@ public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {

@HxRequest
@GetMapping("/owners/{ownerId}")
public ModelAndView htmxShowOwner(@PathVariable("ownerId") int ownerId, HttpServletResponse response) {
response.addHeader("HX-Push-Url", "/owners/" + ownerId);
public ModelAndView htmxShowOwner(@PathVariable("ownerId") int ownerId) {
return handleShowOwner(ownerId, "fragments/owners :: details");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.springframework.web.bind.annotation.RequestMapping;

import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
Expand Down Expand Up @@ -87,9 +85,7 @@ public String initCreationForm(Owner owner, ModelMap model) {

@HxRequest
@GetMapping("/pets/new")
public String htmxInitCreationForm(Owner owner, ModelMap model, HttpServletRequest request,
HttpServletResponse response) {
response.addHeader("HX-Push-Url", request.getServletPath());
public String htmxInitCreationForm(Owner owner, ModelMap model) {
return handleInitCreationForm(owner, model, FRAGMENTS_PETS_EDIT);
}

Expand Down Expand Up @@ -135,8 +131,7 @@ public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, Mode
@HxRequest
@GetMapping("/pets/{petId}/edit")
public String htmxInitUpdateForm(@PathVariable("ownerId") int ownerId, Owner owner,
@PathVariable("petId") int petId, ModelMap model, HttpServletResponse response) {
response.addHeader("HX-Push-Url", "/owners/" + ownerId + "/pets/" + petId + "/edit");
@PathVariable("petId") int petId, ModelMap model) {
return handleInitUpdateForm(owner, petId, model, FRAGMENTS_PETS_EDIT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.springframework.web.bind.annotation.PostMapping;

import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
Expand Down Expand Up @@ -85,8 +83,7 @@ public String initNewVisitForm() {

@HxRequest
@GetMapping("/owners/{ownerId}/pets/{petId}/visits/new")
public String htmxInitNewVisitForm(HttpServletRequest request, HttpServletResponse response) {
response.addHeader("HX-Push-Url", request.getServletPath());
public String htmxInitNewVisitForm() {
return FRAGMENTS_PETS_VISITS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.web.bind.annotation.ResponseBody;

import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
* @author Juergen Hoeller
Expand All @@ -46,34 +45,31 @@ public VetController(VetRepository clinicService) {
}

@GetMapping("/vets.html")
public String showVetList(@RequestParam(defaultValue = "1") int page, Model model, HttpServletResponse response) {
return handleVetList(page, model, "vets/vetList", response);
public String showVetList(@RequestParam(defaultValue = "1") int page, Model model) {
return handleVetList(page, model, "vets/vetList");
}

@HxRequest
@GetMapping("/vets.html")
public String htmxShowVetList(@RequestParam(defaultValue = "1") int page, Model model,
HttpServletResponse response) {
return handleVetList(page, model, "fragments/vets :: list", response);
public String htmxShowVetList(@RequestParam(defaultValue = "1") int page, Model model) {
return handleVetList(page, model, "fragments/vets :: list");
}

protected String handleVetList(int page, Model model, String view, HttpServletResponse response) {
protected String handleVetList(int page, Model model, String view) {
// Here we are returning an object of type 'Vets' rather than a collection of Vet
// objects so it is simpler for Object-Xml mapping
Vets vets = new Vets();
Page<Vet> paginated = findPaginated(page);
vets.getVetList().addAll(paginated.toList());
return addPaginationModel(page, paginated, model, view, response);
return addPaginationModel(page, paginated, model, view);
}

private String addPaginationModel(int page, Page<Vet> paginated, Model model, String view,
HttpServletResponse response) {
private String addPaginationModel(int page, Page<Vet> paginated, Model model, String view) {
List<Vet> listVets = paginated.getContent();
model.addAttribute("currentPage", page);
model.addAttribute("totalPages", paginated.getTotalPages());
model.addAttribute("totalItems", paginated.getTotalElements());
model.addAttribute("listVets", listVets);
response.addHeader("HX-Push-Url", "/vets.html");
return view;
}

Expand Down
14 changes: 9 additions & 5 deletions src/main/resources/templates/fragments/owners.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>Owners</h2>
<tr th:each="owner : ${listOwners}">
<td>
<a th:href="@{/owners/__${owner.id}__}" th:text="${owner.firstName + ' ' + owner.lastName}"
th:attr="hx-get=@{/owners/__${owner.id}__}" hx-target="#block-content"/></a>
hx:get="@{/owners/__${owner.id}__}" hx:push-url="@{/owners/__${owner.id}__}" hx-target="#block-content"/></a>
</td>
<td th:text="${owner.address}"/>
<td th:text="${owner.city}"/>
Expand Down Expand Up @@ -83,10 +83,10 @@ <h2>Owner Information</h2>
</tr>
</table>

<a th:attr="hx-get=@{__${owner.id}__/edit}" hx-target="#block-content" th:href="@{__${owner.id}__/edit}"
<a hx:get="@{__${owner.id}__/edit}" hx:push-url="@{__${owner.id}__/edit}" hx-target="#block-content" th:href="@{__${owner.id}__/edit}"
class="btn btn-primary">Edit
Owner</a>
<a th:attr="hx-get=@{__${owner.id}__/pets/new}" th:href="@{__${owner.id}__/pets/new}" hx-target="#block-content" class="btn btn-primary">Add
<a hx:get="@{__${owner.id}__/pets/new}" hx:push-url="@{__${owner.id}__/pets/new}" th:href="@{__${owner.id}__/pets/new}" hx-target="#block-content" class="btn btn-primary">Add
New Pet</a>
</div>

Expand Down Expand Up @@ -122,9 +122,13 @@ <h2>Pets and Visits</h2>
</tr>
<tr>
<td><a th:href="@{__${owner.id}__/pets/__${pet.id}__/edit}"
th:attr="hx-get=@{__${owner.id}__/pets/__${pet.id}__/edit}" hx-target="#block-content">Edit Pet</a></td>
hx:get="@{__${owner.id}__/pets/__${pet.id}__/edit}"
hx:push-url="@{__${owner.id}__/pets/__${pet.id}__/edit}"
hx-target="#block-content">Edit Pet</a></td>
<td><a th:href="@{__${owner.id}__/pets/__${pet.id}__/visits/new}"
th:attr="hx-get=@{__${owner.id}__/pets/__${pet.id}__/visits/new}" hx-target="#block-content">Add Visit</a></td>
hx:get="@{__${owner.id}__/pets/__${pet.id}__/visits/new}"
hx:push-url="@{__${owner.id}__/pets/__${pet.id}__/visits/new}"
hx-target="#block-content">Add Visit</a></td>
</tr>
</table>
</td>
Expand Down

0 comments on commit fce19de

Please sign in to comment.