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

标签 spring spring-mvc

请在下面找到代码:

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/

相关文章:

java - 创建一个 Spring bean 保存 ServletRequest 属性

java - 如何知道 Java 应用程序是否使用 MVC 设计模式?

spring - CSS未在Spring Boot中加载

java - @PropertySource 从另一个项目加载另一个属性文件

java - 谁将 Spring ModelMap 作为参数传递给 Controller ​​方法?

spring - 第一个 Spring 批处理作业被第二个作业的详细信息覆盖

java - 将测试类和配置类合并在一个类中?

java - Hibernate注解,Mysql TEXT列,始终创建varchar

java - 如何使用 spring 正确管理 PathVariables

security - Grails LDAP身份验证失败