java - Spring Framework 未映射到我的控制器
问题描述
我有一个弹簧框架的问题,我不确定它是我的日食还是我的罐子,或者我当然......
我有一个组件扫描,有时它有时在 spring-mvc-demo-servlet.xml 中添加包时不起作用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema /beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luis.org.springdemo.mvc" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
现在在 web.xml 中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org /xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这些是我的控制器,其中一些映射和一些不映射,但最有趣的是当我使用新类和控制器更改并重新运行项目时,只有旧控制器工作。
@Controller
@RequestMapping("/Customer")
public class CustomerController{
@RequestMapping("/forms")
public String getForm(Model customerModel) {
customerModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(@Valid @ModelAttribute("customer") Customer
customer, BindingResult theBindingResult) {
System.out.println("The Customer Last Name is : |" + customer.getLastName()+"|");
if(theBindingResult.hasErrors()) {
return "customer-form";
}else {
return "customer-result";
}
}
}
但是这个没有
@Controller
@RequestMapping("/osa")
public class ProductController{
@RequestMapping("/forms")
public String createAModel(Model productModel) {
productModel.addAttribute("product", new Product());
return "product-form";
}
@RequestMapping("/processForm")
public String ProcessModel(@Valid @ModelAttribute("product") Product product, BindingResult
theBindingResult){
if(theBindingResult.hasErrors()) {
return "product-form";
}else {
return "process-product";
}
}
}
解决方案
我弄清楚是什么...
我的 Apache 服务器显然正在缓存最新信息,并且没有运行任何信息。所以我删除并再次运行并工作。
推荐阅读
- java - 以前工作的 Spring Boot 应用程序现在给出 NoClassDefFoundError
- testing - 有没有办法将 testcafe 与我的本地服务器通信?
- algorithm - 找到 DAG 的 k 个随机拓扑类型
- c# - 迭代json子节点c#
- reactjs - 如何使用反应测试库测试流畅的 ui 下拉列表?
- sql - SQL 最大值但也显示来自其他列的值
- docker - Docker容器卷未安装
- stata - 将 spss 导入 Stata 时删除空变量
- reactjs - Next.js getServerSideProps 始终未定义
- python - 投影仪相机外部校准?