gpt4 book ai didi

spring - viewresolver 无法在 spring 中重定向到正确的 View

转载 作者:行者123 更新时间:2023-12-01 05:03:32 25 4
gpt4 key购买 nike

请在下面找到代码:

viewResolver 无法定向到从 Controller 解析的所需 View 。我正在 Controller 中打印 View 名称。打印的 View 名称是正确的。但最终它登陆了一个新的网址!!

详情请往下看!!

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>

在 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

控制台上的最后几行

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}

这些行是打印在 Controller 中的行。我可以看到 View 是 HelloPage但为什么它在浏览器中作为消息消息 /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp

最佳答案

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

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

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

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

我希望这对你有用。

关于spring - viewresolver 无法在 spring 中重定向到正确的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com