Spring“Hello,World”项目配置问题

时间:2015-05-12 15:07:38

标签: java spring maven spring-mvc tomcat

我正在使用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"/>

我的控制器类 - 用一个简单的系统输出(没有触发):

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 /部门,我得到了Tomcat“找不到资源”的屏幕。 我也包括我的目录结构,以防它出现问题。

enter image description here

这是错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

component-scan base-package =&#34; org.springframework.samples.web&#34;

似乎不正确。

由于控制器的包不同,你可能会收到此错误。

将其更改为: component-scan base-package =&#34; ocom.abbas.core.main&#34;

答案 1 :(得分:0)

更改以下内容:

@RequestMapping(value="/departments")
public String simplePattern(){

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

TO:

   @RequestMapping(value="/departments")
    public String simplePattern(){

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

因为我看不到任何名为showResult的JSP。而你甚至没有添加任何模型属性或任何东西。