【发布时间】:2016-04-18 07:39:44
【问题描述】:
我正在尝试在 Spring 调度程序 servlet 中处理请求。
我的 web.xml 有 servlet:
<servlet>
<servlet-name>ApplicationDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringConfig/WebApplication.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ApplicationDispatcher</servlet-name>
<url-pattern>*.json</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>/entities/*</url-pattern>
</servlet-mapping>
我的控制器看起来:
@Controller(value="entities")
public class EntitiesController {
private static final Logger LOGGER = Logger.getLogger(EntitiesController.class);
@Autowired
private IEntityDataService iEntityDataService;
@RequestMapping("/list")
public String displayAllEntities() {
LOGGER.info("Displaying entity dashboard");
return "entity_landing";
}
@RequestMapping("/display")
public String displayCheckpointDashboard(Integer id) {
LOGGER.info("Displaying checkpoint dashboard for id " + id);
return "entity";
}
@RequestMapping("/update")
public String displayUpdateEntity(Integer id) {
System.out.println("Update id " + id);
return "new_entity";
}
@RequestMapping("/add")
public String displayNewEntity() {
LOGGER.info("Displaying new entity page");
return "new_entity";
}
}
我在我的应用程序日志中看到以下日志:
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entity/add/entityDetails.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntityController.saveEntityDetails(com.test.vo.EntityCheckpointsVo)
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entities/list],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayAllEntities()
2016-01-13 16:15:12 INFO RequestMappingHandlerMapping:180 - Mapped "{[/entities/add],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayNewEntity()
2016-01-13 16:15:12 INFO DispatcherServlet:476 - FrameworkServlet 'ApplicationDispatcher': initialization completed in 950 ms
2016-01-13 16:15:12 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:15:17 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:17:04 WARN PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
我没有任何线索,因为日志显示/entities/add 已注册。我可以访问其他 URL,例如 localhost:8080/TestProject/entity/add/entityDetails.do,但我无法访问 localhost:8080/TestProject/entities/add。
请帮帮我。
谢谢
更新:
来自 EntityController 的片段
@Controller
@RequestMapping(value = "/entity")
public class EntityController {
@RequestMapping(value = "/add/entityDetails.do", method = RequestMethod.GET)
public String saveEntityDetails(EntityCheckpointsVo entityCheckpointsVo) {
return "success";
}
}
【问题讨论】:
-
我认为一旦你在 servletMapping 中提到 / 而不是实体,问题就会得到解决。
-
会不会是未声明的 HTTP 方法?我在您的日志中看到“methods=[]”。
-
@Berger 在这种情况下 spring 会告诉方法不支持
-
我注意到您没有使用基本
/来进行调度程序servlet 本身的映射,并且您有多种种类 的映射模式。为什么,是否可以只使用 Spring Boot 并完全跳过web.xml的复杂性? -
@Jango:当我提到 / 作为 url 映射时,
/TestProject/index.html没有被映射。