sts下使用maven 创建第一个springmvc

来源:互联网 发布:阿里云二级域名 端口 编辑:程序博客网 时间:2024/06/15 18:12

        使用公司的框架(基于spring)久了,越来越觉得spring框架的强大。开始整理spring开发的一些细节。从创建到完成各种开发,从点滴记录,也分享给各位观众老爷,希望能帮助到有需要的人,不足之处还望指教!

IDE使用的是sts(http://spring.io/tools/sts),第一次打开它会创建一个默认的Server工程,建议不要把它删掉。下面介绍maven和tomcat等一些基本的相关配置。

maven(官方地址)  的配置过程:

WINDOW-->Preference-->搜索maven-->Installations-->Add

路径指向为本地maven安装路径。接着配置User Settings。


使用默认的setting文件,下载jar包的速度太慢,这里我使用的是阿里巴巴搭建的仓库镜像。具体配置在附件里,大家平时开发时可以使用这个配置提高开发效率。

server的配置。首先先将默认的server删除,再添加一个新的tomcat服务器。 

  


增加JVM的配置是为了防止tomcat出现启动超时,内存溢出等现象。


至此,maven和server的配置完成。

创建第一个spring-mvc工程

File-->new-->Maven Project

成功创建之后可能会有报错:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

原因是未添加server runtime到java build path中去。

解决方法:

右键工程,选择Build Path -->Config Build Path-->Libraries-->Add Library...


点击Apply-->OK之后发现错误不见了。

之后创建springMVC的一些相关配置文件和修改pom.xml的配置。

配置 Maven 使用 Spring 库.
  • 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.im2</groupId>  <artifactId>hestia</artifactId>  <packaging>war</packaging>  <version>1.0.1</version>  <name>hestia 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>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.1.0</version>            <scope>provided</scope>        </dependency>         <!-- Spring dependencies -->        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.1.4.RELEASE</version>        </dependency>         <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.1.4.RELEASE</version>        </dependency>         <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.1.4.RELEASE</version>        </dependency>  </dependencies>  <build>    <finalName>hestia</finalName>  </build></project>

配置 web.xml:
Spring MVC 的 DispatcherServlet将根据原则读取XML配置文件:
<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   id="WebApp_ID" version="3.0">       <display-name>HelloWorldSpring</display-name>       <servlet>       <servlet-name>spring-mvc</servlet-name>       <servlet-class>           org.springframework.web.servlet.DispatcherServlet       </servlet-class>       <load-on-startup>1</load-on-startup>   </servlet>          <servlet-mapping>       <servlet-name>spring-mvc</servlet-name>       <url-pattern>/</url-pattern>   </servlet-mapping>     <!-- Other XML Configuration -->   <!-- Load by Spring ContextLoaderListener -->   <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/root-context.xml</param-value>   </context-param>         <!-- Spring ContextLoaderListener -->   <listener>       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>    <!-- web.xml --><!-- Spring ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <!-- Load by Spring ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>       /WEB-INF/root-context.xml,        </param-value></context-param></web-app>

Spring MVC 的 DispatcherServlet将根据原则读取XML配置文件:
  • {servlet-name} ==> /WEB-INF/{servlet-name}-servlet.xml
  • spring-mvc-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-4.1.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.1.xsd       http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">    <context:component-scan base-package="com.hestia.controller"/>       <context:annotation-config/>       <bean       class="org.springframework.web.servlet.view.InternalResourceViewResolver">               <property name="prefix">           <value>/WEB-INF/pages/</value>       </property>               <property name="suffix">           <value>.jsp</value>       </property>                  </bean>    </beans>

:
在Spring应用程序 ContextLoaderListener 将读取其他 XML 配置文件(如下的 abc.xml 和 root-context.xml 两个文件)。 可能不需要配置 ContextLoaderListener,如果你的应用程序并不需要读取其他XML配置文件。
 
<!-- web.xml -->  <!-- Spring ContextLoaderListener -->   <listener>       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>    <!-- web.xml --><!-- Spring ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <!-- Load by Spring ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>       /WEB-INF/root-context.xml,        </param-value></context-param>
  • /WEB-INF/root-context.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"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">   <!-- Empty --> </beans>
indexController.java
package com.hestia.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class indexController { @RequestMapping("/hello")    public String hello(Model model) {                 model.addAttribute("greeting", "Hello Spring MVC");                 return"index";             }}

WEB-INF/pages/index.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>Spring4 MVC -HelloWorld</title></head><body>    <h1>${greeting}</h1></body></html>
代码片已经全部贴完,再贴一张完整的工程目录结构:
至此,一个简单的入门级别spring MVC代码创建完成。接下来运行代码:
右键servers下的服务器,选择Add And Remove,,将hestia添加到configured中(相当于添加到tomcat的webapps中去)

接着使用Debug模式运行tomcat。
访问对应url可看出springMVC已经可以成功运行。
后续我还会继续更新个人对springMVC的理解和运用事例,并结合一些其他框架(如Mybatis、freemarker等)的运用,尽可能的做到详细以帮助到大家。第一次写博客,花了我好几个小时,没有介绍原理只是单纯的上代码,个人觉得是一个很好的适合新手入门的demo。
代码下载链接
0 0
原创粉丝点击