我想在抽象类中使用通用方法@ModelAttribute,但使用来自子类的值。最终目标是在JSP中检索变量的值。该值在每个子类控制器中都不相同,但我不想复制@ModelAttribute方法。ModelAttribute在抽象类中的值从子类
抽象类
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;
}
}
的sublass,控制器
@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但使用Spring版本3.1.2.RELEASE工作(该值使用子类控制器的值设置)。 如何更新我的代码以使用Spring 4?
你为什么要叫'setPath'再次从'@ PostConstruct'方法?你从构造函数中调用它... –
你说得对。这不是必需的。 – BasicCoder