No mapping found for HTTP request with URI
问题描述:运行一直有No mapping found for HTTP request with URI [/enterprisemana/login] in DispatcherServlet with name 'springDispatcherServlet'的问题。
项目的目录结构:
其中web.xml的配置
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
spring.xml的配置:
<!-- 进行包扫描 -->
<context:component-scan base-package="com.August."/>
</beans>
然后是spring-mvc.xml的配置
<!-- 1.配置注解 -->
<mvc:annotation-driven/>
<!-- 2.配置扫描,扫描指定包中的控制器
<context:component-scan base-package=""></context:component-scan>
扫描器会扫描带有@Component@Service@Controller@Component等注解,并实现相应的操作 -->
<context:component-scan base-package="com.August.controller"/>
<!-- 3.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix代表前缀,suffix代表后缀 -->
<property name="prefix" value="/WEB-INF/user/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 放行如JS这样的静态资源 -->
<mvc:default-servlet-handler/>
然后在登录界面的表单:
<form action="login" method="post">
Account: <input type="text" name="account"/><br/><br/>
Password:<input type="password" name="password"/><br/><br/>
Character:<input type="radio" name="character" value="buyer" checked/>Buyer
<input type="radio" name="character" value="saler"/>Saler
<input type="radio" name="character" value="stockman"/>Stockman
<input type="radio" name="character" value="sysman"/>System Manager
<br/><br/>
<input type="submit" value="Login"/>
</form>
Controller:
package com.August.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.August.entity.User;
import com.August.service.UserService;
@Controller
//@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userservice;
@RequestMapping("login")
public String Login(String account,String password,String character,HttpSession session) {
//删掉了业务操作,直接转发页面
return "buyer";
}
}
输出结果:
DEBUG [http-apr-8080-exec-5] - DispatcherServlet with name 'springDispatcherServlet' processing POST request for [/enterprisemana/login]
**WARN [http-apr-8080-exec-5] - No mapping found for HTTP request with URI [/enterprisemana/login] in DispatcherServlet with name 'springDispatcherServlet'**
DEBUG [http-apr-8080-exec-5] - Successfully completed request