Viewresolver cannot redirect correct view in spring

Enter the code below:

The ViewResolver cannot redirect to the required view passed from the controller. I am printing the name of the view in the controller. correctly typed name. but finally it lands on a new url!

See below for details.

controller

package in.co.linq.StudentAdmissionController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class StudentAdmissionController {

public StudentAdmissionController()
{
    super();
    System.out.println("StudentAdmissionController Constructor!!");
}

@RequestMapping(value="/Register" ,method=RequestMethod.GET)
public ModelAndView getRegisterForm()
{
    ModelAndView modelAndView =new ModelAndView("Register");
    modelAndView.addObject("msg","Register Me");
    System.out.println("In getRegisterForm");
    return modelAndView;
}

@RequestMapping(value="/HelloPage.html",method=RequestMethod.POST)
public ModelAndView submitRegisterForm(@RequestParam("txtname") String name,@RequestParam("txtcollege") String college
        ) {

    ModelAndView modelAndView =new ModelAndView("HelloPage","msg", "Congrats!! Form submitted for "+name +" in college"+college);
    //modelAndView.addObject("msg", "Congrats!! Form submitted for "+name +" in college"+college);
    //modelAndView.addObject("college", college);
    System.out.println(name+college);
    System.out.println("In submitRegisterForm");
    System.out.println(modelAndView.getViewName());
    System.out.println(modelAndView.getModel());
    return modelAndView;
}

@RequestMapping(value="/HelloPage/{countryName}/{userName}",method=RequestMethod.GET)
public ModelAndView method(@PathVariable("userName") String userName,@PathVariable("countryName") String countryName) {

    ModelAndView modelAndView =new ModelAndView("HelloPage","msg", "Congrats!! Form submitted for "+userName+countryName);
    //modelAndView.addObject("msg", "Congrats!! Form submitted for "+name +" in college"+college);
    //modelAndView.addObject("college", college);
    System.out.println(userName+countryName);
    System.out.println("In submitRegisterForm");
    System.out.println(modelAndView.getViewName());
    System.out.println(modelAndView.getModel());
    return modelAndView;
}

      

}

Dispatcher-servlet.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
     <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

      

ERROR found at URL http: // localhost: 8080 / SpringMVCUsingRequestParam / HelloPage.html / India / Nilotpal


HTTP Status 404 - /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp

type Status report

**message /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp**

description The requested resource is not available.

Apache Tom

      

Last few lines on console


INFO: Mapped URL path [/studentadmission/*] onto handler 'studentAdmissionController'
Jun 30, 2015 11:32:54 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'SD-StudentAdmission': initialization completed in 19601 ms

      

Console


NilotpalIndia
In submitRegisterForm
HelloPage
{msg=Congrats!! Form submitted for NilotpalIndia}

      

These lines are the ones printed in the controller. I can see that the view HelloPage

, but why is it accepting a message in the browser /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp

Do

+3


source to share


2 answers


Try importing org.springframework.web.servlet.ModelAndView

instead org.springframework.web.portlet.ModelAndView

of the ModelView class.

As of newer Spring 3 or later, you can directly return the view name as the return value of the method as shown below

@RequestMapping("/HelloPage/{countryName}/{userName}",method=RequestMethod.GET)
public String helloSpring(Model model,@PathVariable("userName") String userName,@PathVariable("countryName") String countryName)
{
    model.addAttribute("msg", "Congrats!! Form submitted for "+userName+countryName);
    return "HelloPage";
}

      



You need to take the model object from the method parameter, what value or object that you need to pass to the view can be added to the model object and just return the view name from the method.

I hope this works for you.

+1


source


you have 404 error for this url

: /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp



your controller is bound to: / HelloPage / {countryName} / {username}

if you can post your jsp or the button you redirect to this url, but maybe if you remove the .html from the view you click on that url it will be ok

0


source







All Articles