摘要中的 ModelAttribute class 具有来自 subclass 的值
ModelAttribute in abstract class with value from subclass
我想要一个在抽象 class 中注释为 @ModelAttribute 的通用方法,但其值来自 subclasses。最终目标是检索 JSP 中变量的值。每个 subclass 控制器的值都不同,但我不想复制 @ModelAttribute 方法。
摘要class
public abstract class BaseController {
protected String PATH = "";
public void setPATH(String inPath) {
PATH = inPath;
}
@PostConstruct
private void init() {
setPATH(PATH);
}
@ModelAttribute("controllerPath")
public String getControllerPath() {
return PATH;
}
}
子类,控制器
@Controller
@RequestMapping(OneController.PATH)
public class OneController extends BaseController {
protected static final String PATH = "/one";
public OneController() {
setPATH(PATH);
}
}
JSP
Value for controllerPath: ${controllerPath}
${controllerPath} 的值在 Spring 版本 4.0 中始终为空。9.RELEASE 但可以使用(该值设置为子 class 控制器的值) Spring 版本 3.1。2.RELEASE。
如何更新我的代码以使用 Spring 4?
您需要在抽象控制器中声明抽象的 ModelAttribute 方法。
public abstract class BaseController {
protected String PATH = "";
public void setPATH(String inPath) {
PATH = inPath;
}
@PostConstruct
private void init() {
setPATH(PATH);
}
@ModelAttribute("controllerPath")
public abstract String getControllerPath();
}
并且在扩展抽象控制器的每个控制器上:
@Controller
@RequestMapping(OneController.PATH)
public class OneController extends BaseController {
protected static final String PATH = "/one";
@Override
public String getControllerPath(){
return PATH;
}
}
更新:
如果您不想在所有控制器中重复新方法:
在你的抽象控制器中
@ModelAttribute("controllerPath")
public String getControllerPath(){
return "";
}
您想覆盖值的位置。添加覆盖注释
@Override
public String getControllerPath(){
return PATH;
}
我想要一个在抽象 class 中注释为 @ModelAttribute 的通用方法,但其值来自 subclasses。最终目标是检索 JSP 中变量的值。每个 subclass 控制器的值都不同,但我不想复制 @ModelAttribute 方法。
摘要class
public abstract class BaseController {
protected String PATH = "";
public void setPATH(String inPath) {
PATH = inPath;
}
@PostConstruct
private void init() {
setPATH(PATH);
}
@ModelAttribute("controllerPath")
public String getControllerPath() {
return PATH;
}
}
子类,控制器
@Controller
@RequestMapping(OneController.PATH)
public class OneController extends BaseController {
protected static final String PATH = "/one";
public OneController() {
setPATH(PATH);
}
}
JSP
Value for controllerPath: ${controllerPath}
${controllerPath} 的值在 Spring 版本 4.0 中始终为空。9.RELEASE 但可以使用(该值设置为子 class 控制器的值) Spring 版本 3.1。2.RELEASE。 如何更新我的代码以使用 Spring 4?
您需要在抽象控制器中声明抽象的 ModelAttribute 方法。
public abstract class BaseController {
protected String PATH = "";
public void setPATH(String inPath) {
PATH = inPath;
}
@PostConstruct
private void init() {
setPATH(PATH);
}
@ModelAttribute("controllerPath")
public abstract String getControllerPath();
}
并且在扩展抽象控制器的每个控制器上:
@Controller
@RequestMapping(OneController.PATH)
public class OneController extends BaseController {
protected static final String PATH = "/one";
@Override
public String getControllerPath(){
return PATH;
}
}
更新:
如果您不想在所有控制器中重复新方法:
在你的抽象控制器中
@ModelAttribute("controllerPath")
public String getControllerPath(){
return "";
}
您想覆盖值的位置。添加覆盖注释
@Override
public String getControllerPath(){
return PATH;
}