帮助配置 Spring。
控制器
@Controller
@RequestMapping("/*")
public class IndexController {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@RequestMapping(method = RequestMethod.GET)
public String printHello(HttpServletRequest request, Model model) {
...
存储库
@Repository
public class JpaRepositoryImpl implements JpaRepository {
@PersistenceContext
private EntityManager emf; // Для внедрения EntityManager
...
好吧,一堆其他必要的类
在 spring-db.xml 中连接的存储库
...
<context:component-scan base-package="ru.bityard.bitrix24.serverAuth.repository"/>
...
spring-mvc.xml 中连接的控制器和服务
...
<context:component-scan base-package="ru.bityard.bitrix24.serverAuth.**.web"/>
<context:component-scan base-package="ru.bityard.bitrix24.serverAuth.**.service"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
...
我在 spring-app.xml 中收集所有其他 bean
这就是我试图在 web.xml 中组合的所有内容
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Spring MVC Application</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-app.xml
classpath:spring-db.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mvc.xml
</param-value>
</init-param>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我收到一个错误:
2019-05-25 11:48:00 DEBUG http-nio-8080-exec-4:DispatcherServlet.java:doService:891 - DispatcherServlet with name 'index' processing GET request for [/index.php]
2019-05-25 11:48:00 DEBUG http-nio-8080-exec-4:AbstractHandlerMethodMapping.java:getHandlerInternal:312 - Looking up handler method for path /index.php
2019-05-25 11:48:00 DEBUG http-nio-8080-exec-4:AbstractHandlerMethodMapping.java:getHandlerInternal:322 - Did not find handler method for [/index.php]
2019-05-25 11:48:00 WARN http-nio-8080-exec-4:DispatcherServlet.java:noHandlerFound:1205 - No mapping found for HTTP request with URI [/index.php] in DispatcherServlet with name 'index'
2019-05-25 11:48:00 DEBUG http-nio-8080-exec-4:FrameworkServlet.java:processRequest:1000 - Successfully completed request
我的配置错误在哪里?
替换
@RequestMapping("/*")
为@RequestMapping("/")
- 星号是多余的。最好在方法之前指定这个注解,而不是整个类通用。
最好在方法之前写:
请参阅Spring 请求映射
我设法解决了这样的问题:
我导入 spring-app.xml 的 spring-db.xml 和 spring-mvc.xml 文件
弹簧应用程序.xml
并像这样设置调度程序
web.xml
收割机开始了……