2 tomolang tomolang 于 2016.09.17 12:59 提问

SpringMVC访问路径错误

图片说明

 @Controller
@RequestMapping("/user")
public class UserController {
    private Map<String, User> users = new HashMap<String, User>();

    public UserController(){
        users.put("zs", new User("zs", "123456", "张三", "123456@qq.com"));
        users.put("ls", new User("ls", "123456", "李四", "123456@qq.com"));
        users.put("ww", new User("ww", "123456", "王五", "123456@qq.com"));
        users.put("zl", new User("zl", "123456", "赵六", "123456@qq.com"));
        users.put("gq", new User("gq", "123456", "狗七", "123456@qq.com"));
    }

    @RequestMapping(value="/users",method=RequestMethod.GET)
    public String list(Model model){
        model.addAttribute("users", users);
        return "user/list";
    }
}

/context:component-scan
mvc:annotation-driven/

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

返回的路径带上了Controller类定义的/user了,怎么去掉...

3个回答

whjianwang
whjianwang   2016.09.17 15:35

你控制器类requestMapping映射了地址,怎么去得掉,可以将该处的requestMapping去掉,然后访问地址改为http://localhost:8080/SpringMVC/users

u013829202
u013829202   Rxr 2016.09.18 11:11

springmvc和struts不同。 struts是基于类映射, springmvc是基于方法的映射。 你需要在方法写就好了。 图片说明图片说明

yxl_num
yxl_num   2016.09.19 15:05

jsp的目录不对吧,list.jsp应该在WEB-INF/jsp/user/list.jsp

Csdn user default icon
上传中...
上传图片
插入图片