eclipse maven新建springMVC项目(原创)

1、配置eclipse maven

2、新建maven项目

3、新建src/main/java,更新pom

<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>exam</groupId>
  <artifactId>exam_3</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>exam_3 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>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_2.5_spec</artifactId>
        <version>1.2</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>exam_3</finalName>
    <plugins>
            <plugin>
                <!-- 编译插件 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- 源码插件 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1</version>
                <!-- 发布时自动将源码同时发布的配置 -->
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
                <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
                    <contextPath>/exam_3</contextPath>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>5555</port>
                            <maxIdleTime>100</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
  </build>
</project>

4、更新项目maven依赖

5、写web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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>exam</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

6、写src/main/resources目录下applicationContext.xml和spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>

<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    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
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <!-- 激活@Controller模式 -->
    <mvc:annotation-driven />
    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
    <context:component-scan base-package="com.demo.web.controller" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

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

7、新增部分代码

public class LoginForm {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

@Controller
 public class LoginController {
     @RequestMapping(value="logind")
     public ModelAndView login(HttpServletRequest request,HttpServletResponse response,LoginForm command ){
         String username = command.getUsername();
         ModelAndView mv = new ModelAndView("/index/index","command","LOGIN SUCCESS, " + username);
         return mv;
     }
 }

index.jsp login.jsp

<%@ 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>Insert title here</title>
 </head>
 <body>
     ${command}
 </body>
 </html>

<%@ 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>Insert title here</title>
</head>
<body>
    <div>
        <form action="logind" methed="get">
            <input type="text" name="username">
            <input type="submit" value="SUBMIT">
        </form>
    </div>
</body>
</html>

根目录index.jsp

<%
     request.getRequestDispatcher("/WEB-INF/jsp/login/login.jsp").forward(request,response); %>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

8、目录结构

9、运行项目

ok..........................(转载请注明出处,谢谢^_- )

时间: 2024-10-09 17:27:38

eclipse maven新建springMVC项目(原创)的相关文章

使用Eclipse maven构建springmvc项目

Eclipse maven构建springmvc项目 Listener 监听器

转 :Eclipse maven构建springmvc项目

一.背景介绍 对于初学者,用maven构建项目并不是一件容易的事,springmvc并不是MVC中的主流,但是本人觉得springmvc比struts要好用,真正做到了零配置.一次使用,就放不下了. 二.准备工作 1.Eclipse 3.7 2.maven 3.Eclipse 需要安装maven插件.url:maven - http://download.eclipse.org/technology/m2e/releases .安装maven-3.0.4.并且选择本地的maven,如下图: 三.

Eclipse maven构建springmvc项目

今天深圳台风来了,降了点,真尼玛热    ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ ̄へ ̄ 今天先从写一个Spring MVC的HelloWorld开始,让我们看看如何搭建起一个Spring mvc的环境并运行程序. 一.Maven环境配置 点击Window -> Preferences 如下图所示,配置settings.xml文件的位置 二.新建Maven项目 2.1 新建项目 File-New-Other,选择Dynamic web project 随后选择工作空间: 选完路径之后,我们

使用Eclipse构建Maven的SpringMVC项目

使用Eclipse构建Maven的SpringMVC项目 ? 用maven的好处: 1 最主要就是自动下载jar包和它所依赖的包, 2保证多人开发时jar版本不同的问题 3 文件结构清晰,java文件,资源文件,测试文件都分的很清楚. 将介绍两种方法:一,直接建立Maven项目方法:二.建立Dynamic Web project转成Maven项目方法. ?? ? ? ?将介绍两种方法:一,直接建立Maven项目方法:二.建立Dynamic Web project转成Maven项目方法. 一.直接

Eclipse运行Maven的SpringMVC项目Run on Server时出现错误:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener的问题解决

错误: 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener at org.apache.catalina.loader.WebappClassLoaderBase.

Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)

Eclipse下Maven新建Web项目步骤 1. 2. 3. 4. 5. 问题描述 最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看! 解决方法 第一种:直接在pom.xml文件中添加jar包支持 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <

Eclipse构建Maven的SpringMVC项目

使用Eclipse构建Maven的SpringMVC项目-northwolf-ChinaUnix博客http://blog.chinaunix.net/uid-20060673-id-3175079.html 在pom.xml中添加所需要的jar包,遇到下图中 问题:"Index downloads are disabled, search results may be incomplete."  参考: Maven - m_linda的专栏 - 博客频道 - CSDN.NEThttp

用Eclipse 搭建一个Maven Spring SpringMVC 项目

1: 先创建一个maven web  项目: 可以参照之前的文章:  用Maven 创建一个 简单的 JavaWeb 项目 创建好之后的目录是这样的; 2: 先配置maven  修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="htt

Eclipse+Maven创建webapp项目

原文地址:http://www.cnblogs.com/candle806/p/3439469.html Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new-->other,如下图找到maven project 2.选择maven project,显示创建maven项目的窗口,勾选如图所示,Create a simple project 3.输入maven项目的基本信息,如下图所示: 4.完成maven项目的创建,生成相应的maven项目结果,如下所