Close

Spring MVC - @PathVariable Examples

Spring MVC 

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.logging.Logger;

@Controller
@RequestMapping("trades")
public class TradeController {

private static final Logger LOGGER = Logger.getLogger(TradeController.class.getName());

@RequestMapping
public String handleTradeRequest (Trade trade, Model model) {
String msg = String.format("trade request. buySell: %s, buyCurrency: %s, sellCurrency: %s",
trade.getBuySell(), trade.getBuyCurrency(),
trade.getSellCurrency());
LOGGER.info(msg);
model.addAttribute("msg", msg);
return "my-page2";
}

@RequestMapping("{buySell}/{buyCurrency}/{sellCurrency}")
public String handleTradeRequest2 (Trade trade, Model model) {
String msg = String.format("trade request. buySell: %s, buyCurrency: %s, sellCurrency: %s",
trade.getBuySell(), trade.getBuyCurrency(),
trade.getSellCurrency());
LOGGER.info(msg);
model.addAttribute("msg", msg);
return "my-page";
}


@RequestMapping("paramTest")
public String handleTradeRequest (@RequestParam("buySell") String buySell,
@RequestParam("buyCurrency") String buyCurrency,
@RequestParam("sellCurrency") String sellCurrency,
Model map) {
String msg = String.format("trade request. buySell: %s, buyCurrency: %s, sellCurrency: %s",
buySell, buyCurrency, sellCurrency);
LOGGER.info(msg);
map.addAttribute("msg", msg);
return "my-page";
}

@RequestMapping("pathTest/{buySell}/{buyCurrency}/{sellCurrency}")
public String handleTradeRequest3 (@PathVariable("buySell") String buySell,
@PathVariable("buyCurrency") String buyCurrency,
@PathVariable("sellCurrency") String sellCurrency,
Model map) {
String msg = String.format("trade request. buySell: %s, buyCurrency: %s, sellCurrency: %s",
buySell, buyCurrency, sellCurrency);
LOGGER.info(msg);
map.addAttribute("msg", msg);
return "my-page";
}
}
Original Post




import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("users")
public class UserController {


@RequestMapping("{id}")
public String handleRequest (@PathVariable("id") String userId, Model map) {
map.addAttribute("msg", "User id " + userId);
return "my-page";

}

//this will fail if code is not compiled with debugging information
@RequestMapping("profiles/{userName}")
public String handleRequest2 (@PathVariable String userName, Model model) {
model.addAttribute("msg", "user profile name : " + userName);
return "my-page";
}

@RequestMapping("{id}/posts/{postId}")
public String handleRequest3 (@PathVariable("id") String userId,
@PathVariable("postId") String postId,
Model model) {
model.addAttribute("msg", "user id : " + userId + ", post id: " + postId);
return "my-page";

}

@RequestMapping("{id}/messages/{msgId}")
public String handleRequest4 (@PathVariable Map<String, String> varsMap, Model model) {
model.addAttribute("msg", varsMap.toString());
return "my-page";
}
}
Original Post




See Also