gpt4 book ai didi

java - spring "Hello, World"项目配置问题

转载 作者:行者123 更新时间:2023-11-28 23:31:12 24 4
gpt4 key购买 nike

我正在从 Spring Web Maven 项目构建一个简单的 Web 应用程序,但我无法让我的注释正常工作。这个项目有一个 Dynamic Web Facet,并且正在部署到 Tomcat 7 服务器。

我的 web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>abbas-tables</display-name>

<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->

<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/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>

还有我的 mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">


<context:component-scan
base-package="org.springframework.samples.web"/>


<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>

还有我的 application-config.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"
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">


<context:component-scan
base-package="org.springframework.samples.service"/>

还有我的 Controller 类 - 带有一个简单的系统输出(未触发):

package com.abbas.core.main;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.servlet.ModelAndView ;
import org.springframework.web.servlet.mvc.AbstractController ;

@Controller
public class AbbasTablesController extends AbstractController{
@RequestMapping(value="/departments")
public String simplePattern(){

System.out.println("simplePattern method was called");
return "someResult";

}

@Override
protected ModelAndView handleRequestInternal( HttpServletRequest arg0, HttpServletResponse arg1 )
throws Exception {

// TODO Auto-generated method stub
return null ;
}
}

但是我转到 localhost:8080/abbas-tables/departments 和 localhost:8080/departments,我看到 Tomcat“找不到资源”屏幕。我也包括了我的目录结构,以防出现问题。

enter image description here

这是错误:

enter image description here

最佳答案

组件扫描基础包="org.springframework.samples.web"

似乎不正确。

由于 Controller 的包不同,您可能会收到此错误。

将其更改为:组件扫描基础包=“ocom.abbas.core.main”

关于java - spring "Hello, World"项目配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com