Skip to content

Commit 7d41303

Browse files
committed
Refactored Controller methods
1 parent ca0f185 commit 7d41303

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Ch04_OutputEscapingJSP/src/main/java/de/dominikschadow/webappsecurity/controller/ContactController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import org.springframework.stereotype.Controller;
24+
import org.springframework.ui.Model;
25+
import org.springframework.web.bind.annotation.ModelAttribute;
2426
import org.springframework.web.bind.annotation.RequestMapping;
2527
import org.springframework.web.bind.annotation.RequestMethod;
2628
import org.springframework.web.bind.annotation.SessionAttributes;
27-
import org.springframework.web.servlet.ModelAndView;
2829

2930
/**
3031
*
@@ -37,9 +38,11 @@ public class ContactController {
3738
private Logger logger = LoggerFactory.getLogger(getClass());
3839

3940
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
40-
public ModelAndView addContact(Contact contact) {
41+
public String addContact(@ModelAttribute Contact contact, Model model) {
4142
logger.info("Firstname: " + contact.getFirstname() + ", Lastname: " + contact.getLastname());
4243

43-
return new ModelAndView("contact", "command", contact);
44+
model.addAttribute("contact", contact);
45+
46+
return "contact";
4447
}
4548
}

Ch04_OutputEscapingJSP/src/main/java/de/dominikschadow/webappsecurity/controller/IndexController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import de.dominikschadow.webappsecurity.domain.Contact;
2121
import org.springframework.stereotype.Controller;
22+
import org.springframework.ui.Model;
2223
import org.springframework.web.bind.annotation.RequestMapping;
23-
import org.springframework.web.servlet.ModelAndView;
2424

2525
import static org.springframework.web.bind.annotation.RequestMethod.GET;
2626

@@ -33,7 +33,9 @@
3333
@RequestMapping(value = "/")
3434
public class IndexController {
3535
@RequestMapping(method = GET)
36-
public ModelAndView index() {
37-
return new ModelAndView("index", "command", new Contact());
36+
public String index(Model model) {
37+
model.addAttribute("contact", new Contact());
38+
39+
return "index";
3840
}
3941
}

0 commit comments

Comments
 (0)