viewresolver无法在spring中重定向到正确的视图

时间:2015-06-30 06:28:01

标签: spring spring-mvc

请找到以下代码:

viewResolver无法指向从控制器处理的所需视图。我在控制器中打印viewname。打印的viewname是正确的。但最后它会出现一个新的网址!!

请在下面找到详细信息!!

控制器

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;
}

}

分派器-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>

在网址上找到了错误 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

控制台上的最后几行


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

控制台


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

这些线是控制器中打印的线。我可以看到视图是HelloPage,但为什么它会将浏览器作为消息消息 /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp

2 个答案:

答案 0 :(得分:1)

尝试导入org.springframework.web.servlet.ModelAndView而不是org.springframework.web.portlet.ModelAndView ModelView类。

从Spring 3或更高版本的新版本开始,您可以直接返回视图名称作为方法返回值,如下所示

@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";
}

您需要从方法参数中获取Model对象,可以将要传递给视图的值或对象添加到Model对象中,只返回方法中的视图名称。

我希望这对你有用。

答案 1 :(得分:0)

你的网址有404错误

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

您的控制器被绑定到
:/ HelloPage / {countryName} / {userName}

如果您可以发布您的jsp或您重定向到该网址的按钮,但是 如果您从视图中删除.html,那么您可以使用该网址