Spring MVC
Spring MVC
Spring-MVC
Spring-MVC
Spring-MVC
Spring-MVC
Work Flow in Spring-MVC
Work Flow in Spring-MVC
Work Flow in Spring-MVC
Work Flow in Spring-MVC
Work Flow in Spring-MVC
package spittr.web;
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spittr</title>
<link rel="stylesheet"
type="text/css"
href="<c:url value="/resources/style.css" />" >
</head>
<body>
<h1>Welcome to Spittr</h1>
<a href="<c:url value="/spittles" />">Spittles</a> |
<a href="<c:url value="/spitter/register" />">Register</a>
</body>
</html>
Writing a simple controller
Testing the controller- HomeControllerTest: tests HomeController
• It calls home() directly and asserts that a String containing the value “home” is
returned. it only tests what happens in the home() method.
package spittr.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import spittr.web.HomeController;
Query Parameter
http://internet.org/process-homepage?number1=23&number2=12
• So in the above URL, the query string is whatever follows the question
mark sign (“?”) i.e (“number1=23&number2=12”) this part. And
“number1=23”, “number2=12” are Query Parameters which are joined by
a connector “&”.
http://internet.org?title=Query_string&action=edit
• In the above URL, the query string is “title=Query_string&action=edit” this
part. And “title=Query_string”, “action=edit” are Query Parameters which
are joined
Query Parameter in Spring MVC
Query Parameter
• Using @RequestParam to get Query parameters
• In a Spring MVC application, you can use the @RequestParam annotation
to accept query parameters in Controller's handler methods.
For examples, suppose you have a web application that returns details of
orders and trades, and you have the following URLs:
• http://localhost:8080/eportal/orders?id=1001
• To accept the query parameters in the above URLs, you can use the
following code in the Spring MVC controller:
@RequestMapping("/orders")
public String showOrderDetails(@RequestParam("id") String orderId, Model model)
{ model.addAttribute("orderId", orderId); return "orderDetails“;
Query Parameter in Spring MVC
Query Parameter
• Using @RequestParam to get Query parameters
• If the name of the query parameter is the same as the name of the variable in the
handler's @RequestParam annotated argument then you can simply
use @RequestParam without specifying the name of a query parameter, Spring will
automatically derive the value
@RequestMapping("/trades")
public String showTradeDetails(@RequestParam String tradeId, Model model)
{ model.addAttribute("tradeId", tradeId); return "tradeDetails"; }
we have just annotated the method parameter tradeId with @RequestParam without
specifying the name of the query parameter because the name of both request
parameter and argument name is the same, i.e., "tradeId“ .
Path parameters in Spring MVC
2. Using @PathVariable annotation to extract values from URI
You can use Spring MVC's @PathVaraible annotation to extract any value
which is embedded in the URL itself. Spring calls it a URI template,
where @PathVariable is used to obtain some placeholders from the URI itself.
URL: http://localhost:8080/book/9783827319333
Now, to extract the value of ISBN number from the URI in your Spring MVC
Controller's handler method, you can use @PathVariable annotation as
shown in the following code:
Similar to @RequestParameter annotation, you also can also omit the value
attribute in @PathVariable annotation, if the name of the path variable's
placeholder in the @RequestMapping annotation is the same as the variable
name in the handler method's @PathVariable annotated parameter
URL: http://localhost:8080/book/9783827319333