使用maven

时间:2017-09-11 16:29:50

标签: spring maven spring-mvc

我正在尝试使用Maven在springmvc中运行一个hello项目,当我在服务器上运行时,我在控制台中遇到异常,找不到类 我的pom.xm就在这里......`



<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.ar.ark</groupId>
  <artifactId>TestSpring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>TestSpring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  <java-version>1.7</java-version>
  <org.springframework-version>4.2.1.RELEASE</org.springframework-version>
  </properties>
  
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>
    
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>2.5.2</version>
</dependency>

    
     <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
      
    <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.3.1.RELEASE</version>
</dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.2.0.RELEASE</version>
</dependency>
    
    
  </dependencies>
  <build>
    <finalName>TestSpring</finalName>
  </build>
</project>
&#13;
&#13;
&#13;

web.xml就在这里

&#13;
&#13;
<web-app 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_3_0.xsd"
      version="3.0">
  <display-name>Archetype Created Web Application</display-name>  
  
  <context-param>
  <param-name>ApplicationContext</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  
  
  <servlet>
  <servlet-name>SpringMVC</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
 
  
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping> 
  
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class>
</listener>
</web-app>
&#13;
&#13;
&#13;

applicationContext.xml就在这里

&#13;
&#13;
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 <context:annotation-config />
 <context:component-scan base-package="com" />
 <mvc:annotation-driven />
 <mvc:resources location="/resources/" mapping="/resources/**" />
 
 <bean  id="dataSource" class="org.apache.commons.dbcp BasicDataSource">
 <!-- <property name="DriverClassName" class="com.mysql.jdbc.Driver" /> -->
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost/spring_test"/>
 <property name="username" value="root" />
 <property name="password" value="" />
 
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
 <property name="dataSource" ref="dataSource" />
 <property name="packagesToScan" value="com.entities"></property>
 <property name="hibernateProperties">
 <!-- <props key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</props> -->
 			<props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.default_schema">spring-test</prop>
                <prop key="format_sql">true</prop>
              <prop key="use_sql_comments">true</prop>
               <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
 </property>
 
 </bean>
 <tx:annotation-driven transaction-manager="transactionManager"/>
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
 
 </bean>
 
</beans>
&#13;
&#13;
&#13;

spring-MVCservlet.xml就在这里

&#13;
&#13;
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewResolver" value="org.springframework.web.servlet.view.UrlBasedViewResolve" />
  <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp"/>
 </bean>
 
 
 
 </beans>
&#13;
&#13;
&#13;

userController.java类在这里..

&#13;
&#13;
package com.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="users")
public class UserController {
	
	@RequestMapping( value="/page", method=RequestMethod.GET)
	public ModelAndView getPage(){
		ModelAndView view= new  ModelAndView("hello");
		return view;
	}

}
&#13;
&#13;
&#13;

异常控制台是...... enter image description here` 我的项目结构是 enter image description here 在浏览器中,我得到了这个异常描述 &#34;请求的资源不可用&#34;

hello是一个普通的JSP页面

1 个答案:

答案 0 :(得分:0)

请求上下文侦听器拼写错误,正确的语法是

<listener>
          <listener-class>
             org.springframework.web.context.request.RequestContextListener
          </listener-class>
</listener>

请分享您尝试过的网址,应该是

http://localhost:<portno>/<projectname>/users/page