Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
viewresolver无法在spring中重定向到正确的视图_Spring_Spring Mvc - Fatal编程技术网

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

viewresolver无法在spring中重定向到正确的视图,spring,spring-mvc,Spring,Spring Mvc,请查找以下代码: viewResolver无法定向到从控制器重新解算的所需视图。我正在控制器中打印viewname。打印的视图名称是正确的。但最后它降落到一个新的网址 请查看下面的详细信息 控制器 package in.co.linq.StudentAdmissionController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVa

请查找以下代码:

viewResolver无法定向到从控制器重新解算的所需视图。我正在控制器中打印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;
}
}

Dispatcher servlet.xml

控制台上的最后几行


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}
控制台


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


你的url有404错误吗

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

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

如果您可以发布jsp或重定向到该url的按钮,但是
或者,如果从点击该url的视图中删除.html,则可以尝试导入
org.springframework.web.servlet.ModelAndView
,而不是
org.springframework.web.portlet.ModelAndView
ModelView类

由于新版本的Spring3或更高版本,您可以直接将视图名称作为方法返回值返回,如下所示

@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";
}
您需要从方法参数中获取模型对象,要传递给视图的值或对象可以添加到模型对象中,只需从方法返回视图名称即可


我希望这对您有用。

发布您的控制器,而不是代码片段。谢谢..编辑后发布..@user1233600检查下面的答案。实际上,我正在使用此URL直接点击地址栏…没有使用按钮。。。我发现。。进入viewresolver的viewname的格式为/HelloPage/{countryName}/{userName}ie:/HelloPage/India/Nilotpal。。。。因此,其附加的.jsp使其成为/HelloPage/India/Nilotpal.jsp。。。有什么问题我没法发现!!谢谢!!导入中的更改对我有效:)你能告诉我org.springframework.web.portlet.ModelAndView包的用途吗?请参阅