2017-04-07 158 views
0

您好傢伙,我在啓動服務器後出現以下錯誤。任何人都可以請幫我解決如何處理這個錯誤?我正在使用ECLIPSE Luna和彈簧 4.0.3。是否有任何與特定彈簧版本eclipse LUNA我需要使用?警告:在名爲'appServlet'的DispatcherServlet中沒有找到具有URI [/ SpringMVCHibernate /]的HTTP請求的映射

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringMVCHibernate/] in DispatcherServlet with name 'appServlet' 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.journaldev.spring</groupId> 
    <artifactId>SpringMVCHibernate</artifactId> 
    <name>SpringMVCHibernate</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.6</java-version> 
     <org.springframework-version>4.0.3.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.7.4</org.aspectj-version> 
     <org.slf4j-version>1.7.5</org.slf4j-version> 
     <hibernate.version>4.3.5.Final</hibernate.version> 
    </properties> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${org.springframework-version}</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 

     <!-- Hibernate --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <!-- Apache Commons DBCP --> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <!-- Spring ORM --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 

     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> 

    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
     <finalName>${project.artifactId}</finalName> 
    </build> 
</project> 

WEB.XML

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

servlet的context.xml中
webapp-> WEB-INF->彈簧> APP-servlet-> servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    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 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 



<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory --> 
<beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <beans:property name="url" 
     value="jdbc:mysql://192.168.1.124:3306/mydb" /> 
    <beans:property name="username" value="dbuser" /> 
    <beans:property name="password" value="20root15" /> 
</beans:bean> 

<!-- Hibernate 4 SessionFactory Bean definition --> 
<beans:bean id="hibernate4AnnotatedSessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <beans:property name="dataSource" ref="dataSource" /> 
    <beans:property name="annotatedClasses"> 
     <beans:list> 
      <beans:value>com.journaldev.spring.model.Person</beans:value> 
     </beans:list> 
    </beans:property> 
    <beans:property name="hibernateProperties"> 
     <beans:props> 
      <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect 
      </beans:prop> 
      <beans:prop key="hibernate.show_sql">true</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 

<beans:bean id="personDAO" class="com.journaldev.spring.dao.PersonDAOImpl"> 
    <beans:property name="sessionFactory" 
     ref="hibernate4AnnotatedSessionFactory" /> 
</beans:bean> 

<beans:bean id="personService" 
    class="com.journaldev.spring.service.PersonServiceImpl"> 
    <beans:property name="personDAO" ref="personDAO"></beans:property> 
</beans:bean> 
<context:component-scan base-package="com.journaldev.spring" /> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

<beans:bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <beans:property name="sessionFactory" 
     ref="hibernate4AnnotatedSessionFactory" /> 
</beans:bean> 

PersonController.java
的COM> journaldev->彈簧> PersonController->的Java

package com.journaldev.spring; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.journaldev.spring.model.Person; 
import com.journaldev.spring.service.PersonService; 

@Controller 
public class PersonController { 

    private PersonService personService; 

    @Autowired(required=true) 
    @Qualifier(value="personService") 
    public void setPersonService(PersonService ps){ 
     this.personService = ps; 
    } 

    @RequestMapping(value = "/persons", method = RequestMethod.GET) 
    public String listPersons(Model model) { 
     model.addAttribute("person", new Person()); 
     model.addAttribute("listPersons", this.personService.listPersons()); 
     return "person"; 
    } 

    //For add and update person both 
    @RequestMapping(value= "/person/add", method = RequestMethod.POST) 
    public String addPerson(@ModelAttribute("person") Person p){ 

     if(p.getId() == 0){ 
      //new person, add it 
      this.personService.addPerson(p); 
     }else{ 
      //existing person, call update 
      this.personService.updatePerson(p); 
     } 

     return "redirect:/persons"; 

    } 

    @RequestMapping("/remove/{id}") 
    public String removePerson(@PathVariable("id") int id){ 

     this.personService.removePerson(id); 
     return "redirect:/persons"; 
    } 

    @RequestMapping("/edit/{id}") 
    public String editPerson(@PathVariable("id") int id, Model model){ 
     model.addAttribute("person", this.personService.getPersonById(id)); 
     model.addAttribute("listPersons", this.personService.listPersons()); 
     return "person"; 
    } 

} 
+0

「啓動服務器後」是指?你是否獲得了像'/ SpringMVCHibernate/person'這樣的其他映射? –

回答

1

如果您使用的是IDE(蝕), '在服務器上運行' 選項打開瀏覽器,嘗試加載基本映射(http://localhost:8080/SpringMVCHibernate/)。在正常的Web應用程序中,根據web.xml配置加載主頁(index.html)。

在您的spring控制器中,沒有映射對應於基本映射,如@RequestMapping(value= "/", method = RequestMethod.GET)。由於您的servlet-mappingurl-pattern/,所有請求必須在我的調度程序servlet中處理。但是沒有映射http://localhost:8080/SpringMVCHibernate/

備註:只有在/SpringMVCHibernate/person等其他映射工作正常的情況下,此答案才能解決問題。

+0

我想我需要使像下面'變化 appServlet /人/ ', – Batman

+0

這是問題本身。不能看到任何改變。 –

+0

你能幫我嗎?我如何在spring MVC中使用ajax來驗證表單。特別是當onBlur = functionName()時。 '我已經嘗試過類似下面阿賈克斯$({ \t類型: 「POST」, 網址: '/人/添加', 數據: 「NAME =」 +名字, 成功:函數(響應){ /* $( '#結果')HTML(數據); */ \t如果(response.status == 「SUCCESS」){ \t \t \t \t $( '#信息')HTML(響應) ; \t \t \t} }' – Batman

相關問題