Is it possible to have destination view name configurable in spring mvc 3?
Code snippet is like this:
@Controller
@RequestMapping(value="/test")
public class TestController {
........
@RequestMapping(method=RequestMethod.GET)
public String getCreateForm(Model model) {
model.addAttribute(new AccountBean());
return "newtest";
}
.........
"newtest" is the hard-coded view name. Is it possible to have it configured in an XML-style Spring conf开发者_运维知识库ig file? Thank you!
I guess the real question is how to configure properties of autodiscovered bean via XML.
You can do it by defining a <bean>
with the same name as the autodiscovered one have (when the name of autodiscovered bean is not specified, it's assumed to be a classname with the first letter decapitalized):
@Controller
@RequestMapping(value="/test")
public class TestController {
private String viewName = "newtest";
public void setViewName(String viewName) {
this.viewName = viewName;
}
@RequestMapping(method=RequestMethod.GET)
public String getCreateForm(Model model) {
model.addAttribute(new AccountBean());
return viewName;
}
}
.
<bean id = "testController" class = "TestController">
<property name = "viewName" value = "oldtest" />
</bean>
Another option is to use @Value
with SpEL expressions
@Value("#{testViewName}") private String viewName;
.
<bean id = "testViewName" class = "java.lang.String">
<constructor-arg value = "oldtest" />
</bean>
or property placeholders
@Value("${testViewName}") private String viewName;
.
<context:property-placeholder location = "viewnames" />
viewnames.properties
:
testViewName=oldtest
Well, it is possible to return any string there. So yes - it can be configured.
Update: there are many ways to configure it, one of which (and my preference) being a combination of PropertyPlaceholderConfigurer
and the @Value
annotation, but that was already covered by axtavt.
精彩评论