专注成就辉煌

专注java

SpringMvc&Maven初级篇(一)

开发工具:Eclipse IDE for Java EE Developers
目标:使用SpringMvc框架和Maven配置,开发HelloWorld项目
步骤:
1.新建--Maven Project



2.配置eclipse:
右键项目,选择Project Facets,点击Convert to faceted from
http://dl.iteye.com/upload/attachment/357986/90cb6af7-e66e-36f7-93dd-a8dbb809146e.png,
更改Dynamic Web Module的Version为2.5。(3.0为Java7的)。如果提示错误,可能需要在Java Compiler设置Compiler compliance level 为1.6。或者需要在此窗口的Java的Version改成1.6。
http://dl.iteye.com/upload/attachment/357990/7146caa8-1652-3fbd-8cf1-4eff8d13933a.png,
点击Further configuration available…,弹出Modify Faceted Project窗口此处是设置web.xml文件的路径,我们输入src/main/webapp。Generate web.xml deployment descriptor自动生成web.xml文件,推荐勾选。
设置部署程序集(Web Deployment Assembly):
http://dl.iteye.com/upload/attachment/357992/d8d75b79-e276-3e4b-a121-c172cb00f126.png
 Add -> Java Build Path Entries -> Maven Dependencies -> Finish

设置完成效果图
http://dl.iteye.com/upload/attachment/357996/1def857e-b75a-35f3-ac0b-b7f1c42db7ec.png


3.配置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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>helloworld</name>
<description>helloworld</description>

<properties>
<spring.version>3.0.5.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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Other dependencies -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>
</project>

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" 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_3_0.xsd"
    id
="WebApp_ID" version="3.0">
    
<display-name>helloworld</display-name>
    
<servlet>
        
<servlet-name>dispatcher</servlet-name>
        
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>

    
<servlet-mapping>
        
<servlet-name>dispatcher</servlet-name>
        
<url-pattern>*.html</url-pattern>
    
</servlet-mapping>
    
<welcome-file-list>
        
<welcome-file>index.html</welcome-file>
        
<welcome-file>index.htm</welcome-file>
        
<welcome-file>index.jsp</welcome-file>
        
<welcome-file>default.html</welcome-file>
        
<welcome-file>default.htm</welcome-file>
        
<welcome-file>default.jsp</welcome-file>
    
</welcome-file-list>
</web-app>

dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:mvc
="http://www.springframework.org/schema/mvc"
    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
    "
>
    
    
<!-- 搜索的控制类路径(C) -->
    
<context:component-scan base-package="com.myapps.controllers" />
    
    
<!-- 配置视图路径(V) -->
    
<bean id="viewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
<property name="prefix" value="/WEB-INF/views/"/>
        
<property name="suffix" value=".jsp"/>
    
</bean>
</beans>

在WEB-INF下新建文件夹:views
在views文件夹下新建

helloworldPage.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>helloworldPage.jsp</title>
</head>
<body>
Hello,world.Welcome ${name} here.
</body>
</html>

新建包:com.myapps.controllers

在上面的包内新建类:HelloWorldController.java



package com.myapps.controllers;

import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.servlet.ModelAndView;

@Controller
public class
HelloWorldController {


@RequestMapping(value="/helloworld")

public ModelAndView helloWord() {
return new
ModelAndView("helloworldPage", "name", "hdh");

}

}


 

接着在webapp下新建index.htm(注意后缀是htm,因为如果是html的话,会和web.xml的<url-pattern>*.html</url-pattern>拦截的后缀名冲突,当然也可以新建index.jsp文件)

<!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>index.htm</title>
</head>
<body>
<a href="helloworld.html">hello,world(htm)</a>
</body>
</html>


最后,部署项目,然后启动tomcat服务器,输入http://localhost:8080/helloworld/index.htm
,点击链接进行测试。这样简单的Hello,world入门就完成了。
源码下载地址:/Files/svygh123/helloworld.rar
有不足的地方,请指正,有不明白的地方可以交流,谢谢。

posted on 2012-06-02 23:57 一江东水 阅读(6105) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: