你必须创建两个方法,然后一个具有
@RequestMapping(value = { "/{string:.+}" })注释,另一个具有注释,
@RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" })然后在每个方法中采取相应的措施,因为你不能拥有可选的路径变量。
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;@Controller@RequestMapping("/show")public class HelloController { @RequestMapping(value = { "/{string:.+}" }) public String handleReqShow(@PathVariable String string, @RequestParam(required = false) String name, @RequestParam(required = false) String family, Model model) { System.out.println(string); model.addAttribute("message", "I am called!"); return "hello"; } @RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" }) public String whatever(@PathVariable String string, @PathVariable String mystring, @RequestParam(required = false) String name, @RequestParam(required = false) String family, Model model) { System.out.println(string); System.out.println(mystring); model.addAttribute("message", "I am called!"); return "hello"; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)