Saadat Rizvi Saadat Rizvi - 2 months ago 15
Java Question

RequestToViewNameTranslator doesn't work

I am following the book 'Getting started with the spring framework'(sec. edition).In ch11-bankapp, functions with @ModelAttribute do not return a view name. Hence, the view name is supposed to be returned by RequestToViewNameTranslator.

@Controller
@RequestMapping(value = "/fixedDeposit")
public class FixedDepositController {
private static Logger logger = Logger
.getLogger(FixedDepositController.class);

@Autowired
private FixedDepositService fixedDepositService;

@RequestMapping(value = "/list", method = RequestMethod.GET)
@ModelAttribute(value = "fdList")
public List<FixedDepositDetails> listFixedDeposits() {
logger.info("listFixedDeposits() method: Getting list of fixed deposits");
return fixedDepositService.getFixedDeposits();
}





however, when I enter the url http://localhost:8080/ch11-bankapp/fixedDeposit/list it gives me the HTTP Status 404 - error.
The console gives me the following warning

WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/ch11-session-attributes/fixedDeposit/list] in DispatcherServlet with name 'bankapp'

Can someone please explain me why?

p.s I am running the book's source code. The book assumes that the code is perfect. However it is not working.

Answer

I tried the example and its working like a charm.
Make sure you have done the following

1) Configure the viewNameTranslator

    <bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestTo‌​ViewNameTranslator"/‌​>

Where no view name is provided, the RequestToViewNameTranslator interface >determines the view name automatically. For this you need to configure >DefaultRequestToViewNameTranslator class in Spring MVC configuration file Taken from tutorial mentioned above

2) create a folder fixedDeposit under your WEB-INF/jsp folder (if you have configured some other View resolver than place the fixedDeposit folder inside that folder, for e.g. in my case I used freemarker (WEB-INF/templates), I hope it should work for jsp in the same way)

RequestToViewNameTranslator this is a special bean that resolves the view name out of the request. By default the view name is resolved by removing the URI path from the request and removing the media type. So, for example, if the request is made to http://host:port/context/servlet/some/path/in/the/app.html then by default the view will be resolved to "app" - removing the path and the .html suffix. therefore your view should be http://host:port/context/servlet/some/path/in/the/app

3) Your view file name should be list.jsp (list.ftl in my case) and place your view file with in the new WEB-INF\jsp\fixedDeposit folder

This should work now. Next while positing your questions please give the complete background as well, for e.g. I would really like to see your application-context.xml file as well to help answer your question. if you have root cause exceptions then more detailed stack trace would be more appreciated

Comments