@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 新手刚开始学 请教
解决方案
10
你的路径少了工程名
10
学习了
10
Controller可以加@requestMapping()注解的,表示这个控制器里面的方法前面都+这个/user