请在下面找到代码:

viewResolver无法定向到从控制器解析的所需View。我正在控制器中打印视图名称。打印的视图名称正确。但最终它登陆到一个新的URL!

请在下面找到详细信息!

控制者

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.jsptype 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 initServletBeanINFO: FrameworkServlet 'SD-StudentAdmission': initialization completed in 19601 ms


安慰

NilotpalIndiaIn submitRegisterFormHelloPage{msg=Congrats!! Form submitted for NilotpalIndia}


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

最佳答案

尝试导入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";
}


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

希望这对您有用。

09-30 12:17