「Spring MVC」@PathVariableアノテーションを使用するサンプル

1.Javaコード
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PathVariableExampleController {

/**
* @PathVariable Example:
*
*/
@RequestMapping(“/cft/lijia/{sitePrefix}/{language}/document/{id}/{naturalText}")
public String documentView(Model model,
@PathVariable(value = “sitePrefix") String sitePrefix,
@PathVariable(value = “language") String language,
@PathVariable(value = “id") Long id,
@PathVariable(value = “naturalText") String naturalText) {

model.addAttribute(“sitePrefix", sitePrefix);
model.addAttribute(“language", language);
model.addAttribute(“id", id);
model.addAttribute(“naturalText", naturalText);

String documentName = “Java tutorial for Studyers";
if(id == 8108) {
documentName = “Spring MVC for Studyers";
}

model.addAttribute(“documentName", documentName);

return “documentView";
}
}

2.JSPコード
/WEB-INF/pages/documentView.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8″>
<title>${documentName}</title>
</head>
<body>
<h3>${documentName}</h3>
Site Prefix: ${sitePrefix}
<br> Language: ${language}
<br> ID: ${id}
<br> Natural Text: ${naturalText}
<br>

</body>
</html>

URLの確認
http://localhost:8080/HelloSpringMVC/cft/lijia/default/en/document/8108/spring-mvc-for-beginners

Java

Posted by arkgame