2015-06-30 63 views
3

請在下面找到該代碼:viewresolver無法重定向到春季的正確視圖

viewResolver無法直接從控制器重定向到所需的視圖。我在控制器中打印視圖名稱。所打印的視圖名是正確的。但最終它登陸到一個新的網址!

請在下面找到細節!

控制器

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> 

的錯誤網址上找到控制檯

 

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 
 

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 
 

最後幾行控制檯

 

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

這些行是打印在控制器中的行。我可以看到的看法是HelloPage,但爲什麼它在瀏覽器中以作爲消息消息 /SpringMVCUsingRequestParam/WEB-INF/HelloPage.html/India/Nilotpal.jsp

+0

發佈你的控制器不是一個片段。 –

+0

謝謝..編輯後發佈.. – Nilotpal

+0

@ user1233600檢查下面的答案。 –

回答

1

嘗試導入org.springframework.web.servlet.ModelAndView而不是org.springframework.web.portlet.ModelAndView ModelView類。

由於彈簧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對象,並只從方法返回視圖名稱。

我希望這會對你有用。

+0

非常感謝!導入的變化對我有用:)你能告訴我什麼是org.springframework.web.portlet.ModelAndView的包嗎? – Nilotpal

+0

請參閱[此參考](http://stackoverflow.com/questions/31227339/what-is-the-difference-in-org-springframework-web-servlet-modelandview-vs-org-sp) –

0

你得到了一個404錯誤該URL

:/ SpringMVCUsingRequestParam/WEB-INF/HelloPage.html /印度/ Nilotpal.jsp

控制器被綁定到
:/ HelloPage/{國家名稱}/{用戶名}

如果你可以發表你的jsp或者你重定向到該網址的按鈕,但 propably如果您從你打的網址視圖中的.html這將是確定

+1

其實我直接點擊地址欄上的這個URL ...沒有按鈕...我發現的是... viewresolver的viewname格式爲/ HelloPage/{countryName}/{userName} ie:/HelloPage /印度/ Nilotpal ....因此,它的追加.jsp使它/HelloPage/India/Nilotpal.jsp ...什麼是錯的我無法檢測到! – Nilotpal