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 config file? Thank you!

Answers


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

Need Your Help

How to prioritize IIS URL rewriting rules?

iis url-rewriting

Is it possible to make URL rewriting rules executing with a specified priority order (i.e. one rules before another)?

Is there a way to save window sizing information in wxWidgets 2.8+

c++ wxwidgets

I know there is a way to save Window placement structures in Windows, but I'm wondering if there is an alternative way that can be used for cross platform wxWidgets app.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.