Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spring-petclinic-microservices
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sebastien Briot
spring-petclinic-microservices
Commits
f0bf6927
Commit
f0bf6927
authored
10 years ago
by
michaelisvy
Browse files
Options
Downloads
Patches
Plain Diff
Moving "visit" object to request scope
Previously was in session scope (doesn’t scale as well)
parent
92e7ab43
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/org/springframework/samples/petclinic/web/VisitController.java
+24
-14
24 additions, 14 deletions
...pringframework/samples/petclinic/web/VisitController.java
with
24 additions
and
14 deletions
src/main/java/org/springframework/samples/petclinic/web/VisitController.java
+
24
−
14
View file @
f0bf6927
...
...
@@ -27,12 +27,10 @@ import org.springframework.stereotype.Controller;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.WebDataBinder
;
import
org.springframework.web.bind.annotation.InitBinder
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.SessionAttributes
;
import
org.springframework.web.bind.support.SessionStatus
;
import
org.springframework.web.servlet.ModelAndView
;
/**
* @author Juergen Hoeller
...
...
@@ -41,7 +39,6 @@ import org.springframework.web.servlet.ModelAndView;
* @author Michael Isvy
*/
@Controller
@SessionAttributes
(
"visit"
)
public
class
VisitController
{
private
final
ClinicService
clinicService
;
...
...
@@ -56,32 +53,45 @@ public class VisitController {
public
void
setAllowedFields
(
WebDataBinder
dataBinder
)
{
dataBinder
.
setDisallowedFields
(
"id"
);
}
/**
* Called before each and every @RequestMapping annotated method.
* 2 goals:
* - Make sure we always have fresh data
* - Since we do not use the session scope, make sure that Pet object always has an id
* (Even though id is not part of the form fields)
* @param petId
* @return Pet
*/
@ModelAttribute
(
"visit"
)
public
Visit
loadPetWithVisit
(
@PathVariable
(
"petId"
)
int
petId
)
{
Pet
pet
=
this
.
clinicService
.
findPetById
(
petId
);
Visit
visit
=
new
Visit
();
pet
.
addVisit
(
visit
);
return
visit
;
}
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
@RequestMapping
(
value
=
"/owners/*/pets/{petId}/visits/new"
,
method
=
RequestMethod
.
GET
)
public
String
initNewVisitForm
(
@PathVariable
(
"petId"
)
int
petId
,
Map
<
String
,
Object
>
model
)
{
Pet
pet
=
this
.
clinicService
.
findPetById
(
petId
);
Visit
visit
=
new
Visit
();
pet
.
addVisit
(
visit
);
model
.
put
(
"visit"
,
visit
);
return
"pets/createOrUpdateVisitForm"
;
}
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
@RequestMapping
(
value
=
"/owners/{ownerId}/pets/{petId}/visits/new"
,
method
=
RequestMethod
.
POST
)
public
String
processNewVisitForm
(
@Valid
Visit
visit
,
BindingResult
result
,
SessionStatus
status
)
{
public
String
processNewVisitForm
(
@Valid
Visit
visit
,
BindingResult
result
)
{
if
(
result
.
hasErrors
())
{
return
"pets/createOrUpdateVisitForm"
;
}
else
{
this
.
clinicService
.
saveVisit
(
visit
);
status
.
setComplete
();
return
"redirect:/owners/{ownerId}"
;
}
}
@RequestMapping
(
value
=
"/owners/*/pets/{petId}/visits"
,
method
=
RequestMethod
.
GET
)
public
ModelAndView
showVisits
(
@PathVariable
int
petId
)
{
ModelAndView
mav
=
new
ModelAndView
(
"visitList"
);
mav
.
addObject
(
"visits"
,
this
.
clinicService
.
findPetById
(
petId
).
getVisits
());
return
mav
;
public
String
showVisits
(
@PathVariable
int
petId
,
Map
<
String
,
Object
>
model
)
{
model
.
put
(
"visits"
,
this
.
clinicService
.
findPetById
(
petId
).
getVisits
());
return
"visitList"
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment