Spring RequestMapping 404错误?

时间:2013-11-20 20:50:45

标签: spring spring-mvc spring-3 spring-annotations

如何修复404错误,请求的资源不可用。

来自dispatcher-servlet.xml的viewResolver

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/" p:suffix=".jsp">
 </bean>



@RequestMapping("/Locations/edit/{locationId}")
public void edit(@PathVariable String locationId,ModelMap map) {
    Locations location=new Locations();
    location=locationsService.getByID(Integer.parseInt(locationId));
    map.put("location", location);      
}

错误 HTTP状态404 - /DoctorsBeta/WEB-INF/Locations/edit/1.jsp description请求的资源不可用。

2 个答案:

答案 0 :(得分:2)

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsps/" p:suffix=".jsp">
 </bean>





 @RequestMapping("/Locations/edit/{locationId}")
    public String edit(@PathVariable String locationId,ModelMap map) {
        Locations location=new Locations();
        location=locationsService.getByID(Integer.parseInt(locationId));
        map.put("location", location); 
        reutrn "nameOfYourJspFileHere";
    }

编辑我只是认为您的jsp名称不是1且它是id,因此当您发送/ Locations / edit / 1时

请注意,所有jsps都应该驻留在/ WEB-INF /中(但我建议添加一些其他文件夹(jsps,视图或指示jsp文件存在的东西)dir,所以当你返回视图名时它会追加前缀和后缀。所以jsps文件夹可以有子文件夹,所以每次返回视图名称时都要确保它的路径正确。 例如view name = index.jsp(/WEB-INF/jsps/common/index.jsp)所以在你的方法中你会返回"common/index" 希望它有所帮助。

答案 1 :(得分:0)

Void方法不会返回任何jsp页面,要么返回一个字符串(“jsp的名称”)或页面的ModelAndView

@RequestMapping("/Locations/edit/{locationId}")
    public String edit(@PathVariable String locationId,ModelMap map) {
        Locations location=new Locations();
        location=locationsService.getByID(Integer.parseInt(locationId));
        map.put("location", location); 
        return "test";
    }

如果它会像这样映射

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsps/"
     // test
    p:suffix=".jsp">
 </bean>

它将搜索/WEB-INF/jsps/test.jsp

相关问题