私はいくつかの問題を抱えています。Spring MVC Maven と Hibernate で構成される Spring Tool Suit を使用してアプリケーションを構築しています。私は春に慣れていないにもかかわらず、すべてのコーディングがうまくいっているか、うまくいっていると思います。
ここに私のプロジェクトの2つの画面を置きます:
http://i.stack.imgur.com/AzhOd.png
http://i.stack.imgur.com/fBRId.png
ここに、問題に関与しているファイルについて私が思うものを入れます
aplication-config.mxl:
<?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"/>
</beans>
GeneralController.java:
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import form.Egg;
import service.EggService;
@Controller
//@RequestMapping("/ChickenTest/**")
public class GeneralController {
@Autowired
EggService eggService;
@RequestMapping(value="/index")
public ModelAndView index(){
ModelAndView mav = new ModelAndView();
return mav;
}
//Index of Egg webpage
@RequestMapping(value="/Egg/indexEgg")
public ModelAndView indexEgg(){
ModelAndView mav = new ModelAndView();
return mav;
}
//Action of adding an Egg
@RequestMapping(value="/Egg/addEgg", method = RequestMethod.POST)
public ModelAndView addEgg(@ModelAttribute Egg egg){
ModelAndView mav = new ModelAndView("/index");
Egg eggAux = egg;
eggService.addEgg(eggAux);
return mav;
}
//View list of eggs
@RequestMapping(value="/Egg/viewEggs", method = RequestMethod.POST)
public ModelAndView viewEggs(){
ModelAndView mav = new ModelAndView("/index");
mav.getModelMap().addAttribute("eggList", eggService.viewEggs());
return null;
}
@RequestMapping(value="/Chicken/indexChicken")
ModelAndView indexChicken(){
ModelAndView mav = new ModelAndView();
return mav;
}
@RequestMapping(value="/Farm/indexFarm")
ModelAndView indexFarm(){
ModelAndView mav = new ModelAndView();
return mav;
}
}
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>ChickenTest</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>dispatcherServlet</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>dispatcherServlet</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>
</beans>
JSP の 1 つを入力しようとすると、コンソールに次のような出力が表示されます。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ChickenTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest >>>
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ ChickenTest ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ChickenTest ---
[INFO] Compiling 16 source files to C:\Java\workspace2\ChickenTest\target\classes
[INFO]
[INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest <<<
[INFO]
[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest ---
[INFO] Running war on http://localhost:8080/ChickenTest
[INFO] Using existing Tomcat server configuration at C:\Java\workspace2\ChickenTest\target\tomcat
Nov 08, 2013 12:31:45 AM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Nov 08, 2013 12:31:45 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Nov 08, 2013 12:31:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri Nov 08 00:31:45 ART 2013]; root of context hierarchy
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring/application-config.xml]
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@767ae5: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Nov 08, 2013 12:31:45 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 245 ms
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
Nov 08, 2013 12:31:45 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization started
Nov 08, 2013 12:31:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Fri Nov 08 00:31:45 ART 2013]; parent: Root WebApplicationContext
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-config.xml]
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132299b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@767ae5
Nov 08, 2013 12:31:46 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization completed in 384 ms
Nov 08, 2013 12:31:46 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Nov 08, 2013 12:31:46 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
最後に、この URL http:// localhost:8080/ChickenTest/index.jsp を入力しようとすると、エクスプローラーに次のように表示されます。
HTTP Status 404 - /ChickenTest/index.jsp
type Status report
message /ChickenTest/index.jsp
description The requested resource (/ChickenTest/index.jsp) is not available.
Apache Tomcat/6.0.29
読んでくれてありがとう!さらに何か必要な場合は連絡します!
更新 1:
こんにちは、私はこの解決策を適用しました:
First, your @Controller class is in
package controller;
so your DispatcherServlet config needs to have a component-scan on that package. So change yours to
<context:component-scan base-package="controller"/>
So that the <mvc:annotation-driven> gets applied and your @Controller class is added as a handler.
Now even if you do this, you still don't seem to have a handler method for the path
/ChickenTest/index.jsp
You'll want to either add a <welcome-file> to your web.xml and an index.jsp file in your /webapps folder or add a @RequestMapping handler method that can handler /index.jsp.
しかし、コンソールは GeneralController.class の @Autowire が機能していないことを教えてくれます。
これは私の新しいエラーです:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'generalController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.chicken.service.EggService com.chicken.controller.GeneralController.eggService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.chicken.service.EggService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}