Spring mvc url parameter - spring

I cannot display may restaurant.
I.ve got my controller class:
#Controller
public class RestaurantController extends MultiActionController{
private RestaurantDAO restaurantDAO;
public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
this.restaurantDAO = restaurantDAO;
}
#RequestMapping("/restaurant/{restaurantId}")
public ModelAndView restaurantid(#PathVariable("contactId") int id,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Restaurant restaurant = restaurantDAO.findRestaurantById(id);
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("restaurant", restaurant);
return new ModelAndView("restaurant", modelMap);
}
}
im my jsp just:
<c:out value="${restaurant.name }"
in my spring-servlet.xml:
<bean name="/restaurant/**" class="web.RestaurantController" >
<property name="restaurantDAO" ref="myRestaurantDAO"/>
</bean>

Because you mixed up restaurantId and contactId
#RequestMapping("/restaurant/{restaurantId}")
public ModelAndView restaurantid(#PathVariable("contactId") ...
I guess when you change #PathVariable("contactId") to #PathVariable("restaurantId") it will work.
And add #RequestMapping("/restaurant/**") to your controller:
#RequestMapping("/restaurant/**")
#Controller
public class RestaurantController extends MultiActionController{
BTW: What is a MultiActionController?

Related

javax.servlet.ServletException: Could not resolve view with name '/index' in servlet with name 'dispatcherServlet'

How can I resolve this issue. I am trying to redirect URL by adding trailing slash at the end of each view in spring boot application.
#Configuration
#ComponentScan
#EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter{
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/about", "/about/").setKeepQueryParams(false).setStatusCode(HttpStatus.PERMANENT_REDIRECT);
registry.addRedirectViewController("/index", "/index/").setKeepQueryParams(flase).setStatusCode(HttpStatus.PERMANENT_REDIRECT);
}
}
MyController.java:
#RequestMapping(value= {"/index", "/"},method= {RequestMethod.GET,RequestMethod.POST})
public ModelAndView index(HttpServletRequest request, HttpSession session) {
ModelAndView modelAndView = new ModelAndView("/index/");
return modelAndView;
}
#RequestMapping(value= {"/about"},method= {RequestMethod.GET,RequestMethod.POST})
public ModelAndView about(HttpServletRequest request, HttpSession session) {
ModelAndView modelAndView = new ModelAndView("/about/");
return modelAndView;
}
index.jsp is located on /webapp/views
What I to do Configure anymore?

Is there a way, value defined in one main controller, can be accessed from all the other spring mvc controller?

Something like this:
#Controller
public class HomeController {
public String getCurrentUserDetails() {
String username = "testuser";
return username;
}
}
#Controller
public class DashboardController {
#RequestMapping("/admin/dashboard")
public ModelAndView showDashbard() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username", GET username FROM HOME CONTROLLER [HomeController's getCurrentUserDetails]);
modelAndView.setViewName("/admin/dashboard");
return modelAndView;
}
}
#Controller
public class ProfileController {
#RequestMapping("/admin/profile")
public ModelAndView showProfile() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username", GET username FROM HOME CONTROLLER [HomeController's getCurrentUserDetails]);
modelAndView.setViewName("/profile/dashboard");
return modelAndView;
}
}
Yes I know I can instantiate the HomeController and get the object. But making object in every controller is hectic.
I just want the value of one controller available in all other controllers.

Spring MVC architecture

I have three .jsp pages on my app (index, user and admin). And I have three controllers for everything page but in my administration and user controllers use duplicate methods. For example, I have a method "getGenres" (For shows all genres from DB) in admin controller and user controller. How can I combine these methods if "#RequestMapping" is different in controllers?
#Controller
#EnableWebMvc
#RequestMapping("admin")
public class AdminController {
#Autowired
private GenreTableService genreService;
#RequestMapping(value = "genres")
public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {
Map<String, List<Genre>> genres = new HashMap<>(1);
genres.put("genres", genreService.getAll());
return new ResponseEntity<>(genres, HttpStatus.OK);
}
You want to combine methods. this is sample code.
enter link description here
enter link description here
like this :
#Controller
#RequestMapping("/common")
public class AdminController {
#Autowired
private GenreTableService genreService;
#RequestMapping(value = "/genres")
public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {
Map<String, List<Genre>> genres = new HashMap<>(1);
genres.put("genres", genreService.getAll());
return new ResponseEntity<>(genres, HttpStatus.OK);
}
}
or
#Controller
public class AdminController {
#Autowired
private GenreTableService genreService;
#RequestMapping(value = {"/admin/genres", "/user/genres"})
public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {
Map<String, List<Genre>> genres = new HashMap<>(1);
genres.put("genres", genreService.getAll());
return new ResponseEntity<>(genres, HttpStatus.OK);
}
}

processFormSubmission not being called

I am new to spring. I am creating a simple login page. But the processFormSubmission() is not being called. But the showForm() is working.
public class LoginController extends SimpleFormController
{
private LoginService loginService;
private String loginView;
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public String getLoginView() {
return loginView;
}
public void setLoginView(String loginView) {
this.loginView = loginView;
}
public LoginController() {
setBindOnNewForm(true);
}
#Override
protected ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception
{
TraceUser tr = (TraceUser) command;
System.out.println(tr);
//loginService.
return super.processFormSubmission(request, response, command, errors);
}
#Override
protected ModelAndView showForm(HttpServletRequest request,
HttpServletResponse response, BindException errors)
throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject("traceUser", new TraceUser());
mav.setViewName(getLoginView());
return mav;
}
}
And please help me out with how should the ModelAndView object should be processed further.
First of all, the use of the Controller API has been left aside in favor of the new annotation-based controllers (see the #RequestMapping annotation) and classes like SimpleFormController have been deprecated since quite a while now.
However, to answer your question, I assume your form does not declare method="post" and by default, the SFC will consider only POST requests as form submissions (see the isFormSubmission() method in AbstractFormController). Is this the case ?

Extending the JSTL View class

I am extending the JSTL view class to implement my own view resolver. But, I am having the problem. Look into my code:
public class TestView extends JstlView {
private String fo_suffix = "_jo";
public void setUrl(String url)
{
//We need to change the inputed url to add a prefix for fo
super.setUrl(url.replace("\\.jsp", fo_suffix+ ".jsp"));
}
public void render(Map<String, ?> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
final StringWriter xmlfo = new StringWriter();
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(
response) {
#Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(xmlfo);
}
};
super.render(model, request, wrapper);
In the above code, when i am debugging, the control never comes to the setUrl method. So the url is always null in the internal RequestDispatcher.
Please help me to resolve the issue.
Dont forget to put TestView in "myServletName"-servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="test.TestView"/>
....

Resources