Spring MVC  import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @RequestMapping("trades") public class TradeController {
@RequestMapping("/{trade}") public String handleTradeRequest (Trade trade, Model model) { System.out.println(trade); if (trade.getTradeId() == 0) { model.addAttribute("msg", "No trade found"); return "no-trade-page"; } return "trade-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 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;
/** * Using mutually exclusive regex, which can be used * to avoid ambiguous mapping exception */ @Controller @RequestMapping("/dept") public class DeptController {
@RequestMapping("{id:[0-9]+}") public String handleRequest(@PathVariable("id") String userId, Model model){ model.addAttribute("msg", "profile id: "+userId); return "my-page";
}
@RequestMapping("{name:[a-zA-Z]+}") public String handleRequest2 (@PathVariable("name") String deptName, Model model) { model.addAttribute("msg", "dept name : " + deptName); return "my-page"; } }
Original Post
 /** * This will cause exception : * java.lang.IllegalStateException: Ambiguous handler methods .. */ @Controller @RequestMapping("/employees") public class EmployeeController {
@RequestMapping("{id}") public String handleRequest(@PathVariable("id") String userId, Model model){ model.addAttribute("msg", "employee id: "+userId); return "my-page";
}
@RequestMapping("{employeeName}") public String handleRequest2 (@PathVariable("employeeName") String userName, Model model) { model.addAttribute("msg", "employee name : " + userName); 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
 package com.logicbig.example;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest;
@Controller @ResponseBody public class MyController {
@RequestMapping("/car?/s?o?/info")//score 0 public String test1(HttpServletRequest request) { return "from test1(), request uri: " + request.getRequestURI(); }
@RequestMapping("/c*/s*d/info")//score 2, length = 12 public String test2(HttpServletRequest request) { return "from test2(), request uri: " + request.getRequestURI(); }
@RequestMapping("/card/**")//score 2 but will be used after others because of prefix pattern public String test3(HttpServletRequest request) { return "from test3(), request uri: " + request.getRequestURI(); }
@RequestMapping("/card/{type}/{id:i.*}")//2 template variables + 1 wildcard = score 3 public String test4(@PathVariable String type, @PathVariable String id, HttpServletRequest request) { return "from test4(), request uri: " + request.getRequestURI() + "\n" + "type: " + type + ", id: " + id; }
@RequestMapping("/card/{type}/{id:i.+}")//score 2, length 9 (/card/#/#) public String test5(@PathVariable String type, @PathVariable String id, HttpServletRequest request) { return "from test5(), request uri: " + request.getRequestURI() + "\n" + "type: " + type + ", id: " + id; } }
Original Post
 package com.logicbig.example;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest;
@Controller @RequestMapping("/other/") @ResponseBody public class MyController2 {
@RequestMapping("/c*/s*d/info")//score 2, length = 12, wildcards=2 public String otherTest1(HttpServletRequest request) { return "from otherTest1(), request uri: " + request.getRequestURI(); }
@RequestMapping("/card/{type}/info")//score 1, length 12 (/card/#/info), wildcards=0 public String otherTest2(@PathVariable String type, HttpServletRequest request) { return "from otherTest2(), request uri: " + request.getRequestURI() + "\n" + "type: " + type; } }
Original Post
 package com.logicbig.example;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest;
@Controller @RequestMapping("/another/") @ResponseBody public class MyController3 {
@RequestMapping("/c*/s*d/info")//score 2, length = 12, wildcards=2 public String anotherTest1(HttpServletRequest request) { return "from anotherTest1(), request uri: " + request.getRequestURI(); }
@RequestMapping("/card/{type}/inf{id:.+}")//score 2, length 12 (/card/#/info), wildcard = 0 public String anotherTest2(@PathVariable String type, @PathVariable String id, HttpServletRequest request) { return "from anotherTest2(), request uri: " + request.getRequestURI()+"\n"+ "type: "+type+", id: "+id; } }
Original Post
|
|