当前位置: 代码迷 >> J2EE >> SpringMVC访问不到途径
  详细解决方案

SpringMVC访问不到途径

热度:459   发布时间:2016-04-17 23:58:10.0
SpringMVC访问不到路径

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/add")
    @ResponseBody
    public String addUser(HttpServletRequest request,User user) {
        userService.addUser(user);
        request.setAttribute("user",user);
        return "hello";
    }
}


配置文件

<!-- 注解探测器 -->
    <context:component-scan base-package="com.springapp.mvc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 使用JSP页面进行输出 -->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 这个配置是配置JSP页面的位置 -->
        <property name="prefix" value="/WEB-INF/"/>
        <!-- 指定了表示层的后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="conversionService"
          class="com.springapp.mvc.common.springmvc.IdentityToEntityConversionServiceFactoryBean" />

     <!--配置spirngMVC-web的静态资源,不配会当成控制器-->
    <mvc:resources mapping="/assets/**" location="assets/"/>
    <mvc:resources mapping="/codefans.net/**" location="codefans.net/"/>
    <mvc:resources mapping="/Css/**" location="Css/"/>
    <mvc:resources mapping="/font/**" location="codefans.net/"/>
    <mvc:resources mapping="/Images/**" location="Images/"/>
    <mvc:resources mapping="/Js/**" location="Js/"/>
    <mvc:resources mapping="/Menu/**" location="Menu/"/>
    <mvc:resources mapping="/Node/**" location="Node/"/>
    <mvc:resources mapping="/pages/**" location="pages/"/>
    <mvc:resources mapping="/Public/**" location="Public/"/>
    <mvc:resources mapping="/Role/**" location="Role/"/>
    <mvc:resources mapping="/User/**" location="User/"/>



    <!-- 处理文件上传处理 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"/>

</beans>


访问localhost:8080/user/add 报404 新人初学 求教
------解决方案--------------------
你的路径少了工程名
------解决方案--------------------
学习了
------解决方案--------------------
引用:
类不需要加@requestMapping()注解吧。
方法上面加注解,应该是这样子的吧@RequestMapping("/xxx.do")

Controller可以加@requestMapping()注解的,表示这个控制器里面的方法前面都+这个/user
  相关解决方案