Spring MVC项目没有打开视图

时间:2017-04-10 15:46:45

标签: java xml spring maven spring-mvc

我刚接触弹簧框架我实际上正在尝试创建一个spring mvc电子商务项目。我已经更新了所有内容并且没有出现任何错误但是我没有获得登陆页面而是获得了404! 这是我的堆栈跟踪...

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" 
version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml,/WEB-INF/application-
context.xml</param-value>
    </context-param>

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

    <display-name>Archetype Created Web Application</display-name>

    <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/dispatcher-servlet.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>

的pom.xml

    <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.pat</groupId>
    <artifactId>archangel</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>archangel Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
            <spring.version>4.0.1.RELEASE</spring.version>
    </properties>

<dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.191</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.5.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
    </dependency>

    <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
    </dependency>

    <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.version}</version>
    </dependency>

    <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.version}</version>
    </dependency>

</dependencies>

    <build>
        <finalName>archangel</finalName>
    </build>

</project>

分派器-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.pat.controllers.*" />
<mvc:annotation-driven/>
<bean 
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

</beans>

应用-context.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"
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       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/util  
   http://www.springframework.org/schema/util/spring-util.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.xsd">

<context:component-scan base-package="com.pat.*"/>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" 
  destroy-method="close">
     <property name="driverClassName" value="org.h2.Driver" />
     <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
     <property name="username" value="sa" />
     <property name="password" value="sa" />
</bean>

<bean id="sessionFactory"
 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">  
              <list>  
                  <value>com.pat.*</value>  
              </list>  
    </property> 

    <property name="hibernateProperties">
        <props>
            <prop 
key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

</bean>

<tx:annotation-driven/>

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

<bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

</beans>

DroidController.java

package com.pat.controllers;

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 com.pat.model.User;
import com.pat.services.UserService;

@Controller
public class DroidController
{ 

    public DroidController() 
    {
        System.out.println("inside driod ctrlr.......");
    }

    @Autowired
    UserService userService;
    String message = "Welcome to Spring MVC!";

    @RequestMapping("/")
    public ModelAndView showMessage() 
    {
        System.out.println("in controller");
        ModelAndView mv = new ModelAndView("homepage");
        return mv;
    }

    @RequestMapping("/login")
    public ModelAndView Message() 
    {
        System.out.println("in controller 1");
        ModelAndView mv1 = new ModelAndView("login");
        return mv1;
    }


    @RequestMapping("/signup")
    public String gotoindex(@ModelAttribute("mand")User mand)
    {
        System.out.println("in controller 2");
        return "signup";
    }

    @RequestMapping(value="saveuser",method=RequestMethod.POST)
    public String gotoSignUp(@ModelAttribute("usrform")User usrform)
    {
        userService.insertRow(usrform);
        return "homepage";
    }

}

控制台

Apr 10, 2017 8:26:53 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:archangel' did not find a matching property.
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.38
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 6 2016 20:51:55 UTC
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.38.0
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_102
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_102-b14
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\Patrick Aditya\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         E:\NIIT SWs\apache-tomcat-8.0.38\apache-tomcat-8.0.38
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Patrick Aditya\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=E:\NIIT SWs\apache-tomcat-8.0.38\apache-tomcat-8.0.38
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Patrick Aditya\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=E:\NIIT SWs\apache-tomcat-8.0.38\apache-tomcat-8.0.38\endorsed
Apr 10, 2017 8:26:53 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Apr 10, 2017 8:26:53 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_102\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_111/bin/server;C:/Program Files/Java/jre1.8.0_111/bin;C:/Program Files/Java/jre1.8.0_111/lib/amd64;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Java\jdk1.8.0_102\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;;E:\NIIT SWs\eclipse;;.
Apr 10, 2017 8:26:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8007"]
Apr 10, 2017 8:26:54 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 10, 2017 8:26:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8010"]
Apr 10, 2017 8:26:54 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 10, 2017 8:26:54 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3158 ms
Apr 10, 2017 8:26:54 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 10, 2017 8:26:54 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.38
Apr 10, 2017 8:26:58 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 10, 2017 8:26:58 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Apr 10, 2017 8:26:58 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Apr 10, 2017 8:26:58 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Apr 10, 2017 8:26:58 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Mon Apr 10 20:26:58 IST 2017]; root of context hierarchy
Apr 10, 2017 8:26:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Apr 10, 2017 8:26:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/application-context.xml]
Apr 10, 2017 8:27:00 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Apr 10, 2017 8:27:01 PM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.pat.controllers.DroidController.showMessage()
Apr 10, 2017 8:27:01 PM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/signup],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.pat.controllers.DroidController.gotoindex(com.pat.model.User)
Apr 10, 2017 8:27:01 PM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/saveuser],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.pat.controllers.DroidController.gotoSignUp(com.pat.model.User)
Apr 10, 2017 8:27:01 PM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.pat.controllers.DroidController.Message()
Apr 10, 2017 8:27:01 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.3.4.Final
inside driod ctrlr.......
Apr 10, 2017 8:27:03 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
Apr 10, 2017 8:27:03 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 10, 2017 8:27:03 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Apr 10, 2017 8:27:04 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Apr 10, 2017 8:27:06 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 7959 ms
Apr 10, 2017 8:27:06 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Apr 10, 2017 8:27:06 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Apr 10, 2017 8:27:06 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Apr 10 20:27:06 IST 2017]; parent: Root WebApplicationContext
Apr 10, 2017 8:27:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Apr 10, 2017 8:27:06 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Apr 10, 2017 8:27:06 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 246 ms
Apr 10, 2017 8:27:06 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8007"]
Apr 10, 2017 8:27:06 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8010"]
Apr 10, 2017 8:27:06 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 11891 ms
Apr 10, 2017 8:27:07 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/archangel/] in DispatcherServlet with name 'dispatcher'
Apr 10, 2017 8:27:26 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/archangel/] in DispatcherServlet with name 'dispatcher'

2 个答案:

答案 0 :(得分:0)

缺少webapp,如果您使用的是tomcat,则需要将war文件复制到war文件夹,如果您的test文件名为http://localhost:8080/test/,则需要请致电const io = require('socket.io')(3000); io.on('connection', function (socket) { console.log('a client has connected'); socket.on('clicked', function() { io.emit('clicked'); }); socket.on('clicked1', function() { io.emit('clicked1'); }); }); console.log('socket.io server started at port 3000'); 获取您的主页,但我强烈建议您不要以这种方式学习弹簧,首先尝试symlinked,这是弹簧的新方法,更简单。

答案 1 :(得分:0)

将以下代码用于Application-context.xml路径。

.sas7bdat