码迷,mamicode.com
首页 > 编程语言 > 详细

Eclipse+Maven+springmvc+HelloWorld--Eclipse Maven springMVC第一个HelloWorld吐血配置整理

时间:2016-11-06 22:44:57      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:lap   nap   message   build   write   call   org   ann   apach   

Searched Internet for several days for this god dammed "hello world" startup. Those guides are more or less the same. I wonder whether the authors really understand all details behind this, or just copy+paste the content from the network and add to their blog.

However, after a long struggling trial & error time, i can make it work finally. Here to take some notes.

Key learning: Eclipse will tell you most of the things. So pay attention to the Console output. You can search the internet for those error messages if you are not sure what happened. Once you solve all the Exceptions/Errors, then in most conditions, you project is up & running!

Part I - Env Installation

1. Package versions:

Eclipse 3.7.2 (Indigo) + Java EE plugin;

Maven 3.2.1;

Built in Maven (m2eclipse): 0.10.2

Java JDK 1.7

Tomcat 7

SpringMVC 3.1.1 Release

2. Software Installation

Search internet for details. Nothing special.

Note: to configure maven env path, java path must be configured ahead.

Better to download a local archetype-catalog.xml.

Brief steps: Install JDK & configure path -> Unzip Maven, configure path, configure settings.xml for local jar -> Install Tomcat -> Unzip Eclipse -> configure preference: maven(external maven), java build path, tomcat.

Part II. Project Setup

1. New -> Maven Project -> local catalog -> webapp -> give a group ID & artifact ID.

2. Right click -> Properties

  a. Java build path: src/main/java & src/main/source -> target/classes.

  b. Java compiler: 1.7

  c. Project facets: Dynamic Web Module 3.0 -> further configuration, change WebContect to src/main/webapp. (TBD: the further configuration will disappear then. If you made wrong config, you can check whether you can change it in .settings folder within project.)

 

3. Modify pom.xml.

pom xml will be used to determine the required libraries within this maven project. It will download files from network to your local Maven repo. You can check the network http://mvnrepository.com/artifact to search the dependency details.

Due to network connection issues / package dependency orders, you might not be able to get this download complete for the first time. You will see errors like Missing *** jar:compile; In this case, you can modify the dependency order(this will be complicated), or remove all the *.lastUpdate file, and build again.

You can also download jar files and put into your maven repo. Remember to change the file name to keep consitant with your pom.xml file.

A sample xml is like below: Note, you cannot proceed with your project unless you solve the errors in 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>mvn</groupId>
  <artifactId>spring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    
    
    
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.12</version>
    </dependency>
    
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>servlet-api-2.5</artifactId>
        <version>6.0.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
     
     <!--   
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.4.Final</version>
        </dependency>
     -->
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
        </dependency>
    
  </dependencies>
  <build>
    <finalName>spring</finalName>
  </build>
</project>
View Code

4. Try to check the Deployment Assembly, see if it is consistant with your folder hierarchy. But looks like when I solve the issues eventually, The Maven Dependency is no longer used then. Nothing is assembled to the lib folder now. To be researched.

技术分享

Part III. Write Code

 1. Create xml files, folders like below:

 技术分享

2. Modify web.xml:

A sample is like below.

Key learning: All dependencies must be solved. Because you might copy the pom.xml from others, but here web.xml might contain something which is not in your pom.xml. In this case, tomcat still can start & run, but there will be exceptions. If you just ignore these messages (there are lots of other messages), it will take you lots of time to know what is wrong with your code.

Here in my example: <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> requires package: <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->. But it is NOT in my original pom file. The same condition as <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->, which I also find from Console exceptions.

技术分享
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>springmvc-chapter2</display-name>


    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/ContextLoaderListener.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- POST CHN -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    
</web-app>
View Code

3. ContextLoaderListener.xml. Just paste. Nothing is customized.

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

</beans>
View Code

4. spring-servlet.xml. Just pay attention to the naming convention inside web.xml. [servletname]-servlet.xml. Nothing special.

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- Controller -->
    <bean name="/hello" class="web.controller.HelloWorldController"/>
    
</beans>
View Code

5. HelloWorldController.java. If there is anything wrong with your 3rd party lib, eclipse will tell you here (for example you cannot import a package in your code.)

技术分享
package web.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

//import org.springframework.web.context.ContextLoaderListener;s

public class HelloWorldController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        
        ModelAndView mv = new ModelAndView();
        
        mv.addObject("message", "Hello World!");
        
        mv.setViewName("hello");
        return mv;
    }
}
View Code

6. hello.jsp. Very simple

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<div>Here is the content from controller</div>
${message }
</body>
</html>
View Code

7. Run in Tomcat!

Key learning: Here i made a very stupid mistake. In order to speed up eclipse, i unchecked the "build automatically" option some time ago. And when I tried to run, eclipse told me it could not find HelloWorldController class. How come? Then I realized I did not build the project. There was no class file at all.

技术分享

 

Eclipse+Maven+springmvc+HelloWorld--Eclipse Maven springMVC第一个HelloWorld吐血配置整理

标签:lap   nap   message   build   write   call   org   ann   apach   

原文地址:http://www.cnblogs.com/anbosun/p/6036460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!