在Intellij中设置Spring MVC Maven“Hello World”webapp

时间:2015-01-27 03:12:01

标签: java maven spring-mvc tomcat intellij-idea

我正在尝试在Intellij中设置一个spring webapp。我正在使用Maven来管理依赖项和本地Tomcat Web服务器。到目前为止,我已经使用Spring MVC模板创建了该项目。

我基本上遵循本教程,但使用Intellij 13.1.6并使用Spring MVC模板: http://alfasin.com/setting-up-spring-web-project-on-intellij-using-maven/

我认为这可以让我得到一个" Hello World"应用程序快速运行。但是,我遇到了500个内部错误,我无法解决问题。

当我尝试运行应用程序时,我通常会收到此错误: Class Not Found

看似随机,我有时会得到这个错误: Unable to compile class

这是我的web.xml文件:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是我的mvc-dispatcher-servlet.xml文件:

<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="com.maynard.pipes"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

这是我的控制者:

package com.maynard.pipes;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController 
{   
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model)
    {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
}

这是我的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.springapp</groupId>
<artifactId>project-pipes</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>project-pipes</name>

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

<dependencies>
    <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>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

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

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

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <finalName>project-pipes</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Tests.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

这是我的Tomcat配置: tomcat

如果有人能够指出我所犯的错误,我将不胜感激。我仍然需要在开发的配置方面学到很多东西。感谢。

8 个答案:

答案 0 :(得分:1)

您使用的JSP版本与Tomcat 8版本不兼容。 看看这个:http://tomcat.apache.org/whichversion.html

答案 1 :(得分:0)

我已经通过降级从Tomcat 8降级到Tomcat 7来解决了这个问题。我不确定为什么会这样,但我现在可以看到&#34; Hello World&#34;。

答案 2 :(得分:0)

   package com.abc.controller;
   import org.apache.log4j.Logger;
   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("/hello")
    public class HelloWorldController {
      Logger logger = Logger.getLogger("HelloWorldController"); 
       @RequestMapping(method=RequestMethod.GET)
     public ModelAndView helloController() {
      logger.info("inside Controller");
      String message = "Hello World";
      return new ModelAndView("HelloWorldPage", "message", message);
    }

}

答案 3 :(得分:0)

           <?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:p="http://www.springframework.org/schema/p"  

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/context  

               http://www.springframework.org/schema/context/spring-context-        3.0.xsd"> 

           <context:component-scan  base-package="com.abc.controller" />  


  <bean  
                                                                                class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/views/" />  
    <property name="suffix" value=".jsp" />  

</bean>  

答案 4 :(得分:0)

    ImageView imageView = findViewById(R.id.imageview_scroll);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scroll);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
    bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);

    imageView.setImageBitmap(bitmap);

答案 5 :(得分:0)

 <?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">
         <display-name>Spring_Annotation</display-name>
      <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
                  </welcome-file-list>

           <servlet>
             <servlet-name>spring</servlet-name>
           <servlet-
      class>org.springframework.web.servlet.DispatcherServlet</servlet-
 class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/spring-servlet-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
           </servlet>
          <servlet-mapping>
         <servlet-name>spring</servlet-name>
          <url-pattern>/</url-pattern>
               </servlet-mapping>
                  </web-app>

答案 6 :(得分:0)

         <html>
         <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
              8859-1">
        <title>Index Page</title>
              </head>
         <body>
           <a href="hello.html">CLICH HERE</a>  
            </body>
             </html>

答案 7 :(得分:0)

        <html>
            <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-
                   8859-1">
                   <title>Success Page</title>
                         </head>
                    <body>
                <h2>${message}</h2>
                      </body>
                              </html>