J2EE开发环境配置指南

Written .

需要使用到的软件

开发前要优化的部分

  • ~/.m2/目录下新增settings.xml文件内容如下,此文件会让之后加载的maven包全部从内网服务器内获取,增快访问速度。

    <settings>
      <mirrors>
        <mirror>
          <!--This sends everything else to /public -->
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <url>http://192.168.6.192:8081/nexus/content/groups/public</url>
        </mirror>
      </mirrors>
      <profiles>
        <profile>
          <id>nexus</id>
          <!--Enable snapshots for the built in central repo to direct -->
          <!--all requests to nexus via the mirror -->
          <repositories>
            <repository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
            </repository>
          </repositories>
         <pluginRepositories>
            <pluginRepository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
            </pluginRepository>
          </pluginRepositories>
        </profile>
      </profiles>
      <activeProfiles>
        <!--make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
      </activeProfiles>
    </settings>
    
  • 修改eclipse支持utf8编码 window->Preferences->General->workspace->Text file encoding->Other:UTF-8

utf8

hello world spring mvc

  • File->New->Spring Project 在弹出的对话窗体内输入你的项目名,并在模板中选择 Spring MVC Project

sts-1

  • Next后在对话框内输入项目所在的包

sts-2

  • Spring MVC项目目录结构

spring mvc project

使用Maven向项目添加包引用

  • 使用内网Maven代理来搜索你要使用的包本向导使用velocity为例子

search maven pack

让你的Spring MVC项目支持velocity模板引擎

  • 修改项目中servlet-context.xml文件,注释掉之前的jsp节点增加velocityConfigvelocityViewResolver节点。修改后文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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">
    
    
    
    &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
    
    
    &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
    &lt;annotation-driven /&gt;
    
    
    &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
    &lt;resources mapping="/resources/**" location="/resources/" /&gt;
    
    
    &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
    &lt;!-- 
    &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
        &lt;beans:property name="suffix" value=".jsp" /&gt;
    &lt;/beans:bean&gt;
     --&gt;
    &lt;context:component-scan base-package="cn.gyyx.java" /&gt;
    &lt;!-- velocity --&gt;
    &lt;beans:bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&gt;  
        &lt;beans:property name="resourceLoaderPath" value="/WEB-INF/views" /&gt;  
        &lt;beans:property name="configLocation" value="classpath:common/velocity.properties" /&gt;  
    &lt;/beans:bean&gt;  
    
    
    &lt;beans:bean id="velocityViewResolver"  
        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"&gt;  
        &lt;beans:property name="suffix" value=".vm" /&gt;  
        &lt;beans:property name="contentType" value="text/html;charset=UTF-8" /&gt;
    &lt;/beans:bean&gt;
    
    </beans:beans>
  • resources/common目录下创建velocity.properties内容为

    #encoding  
    input.encoding  =UTF-8  
    output.encoding=UTF-8  
    contentType=text/html;charset=UTF-8  
    
    
    #autoreload when vm changed  
    file.resource.loader.cache=false  
    file.resource.loader.modificationCheckInterval  =1  
    velocimacro.library.autoreload=false
    
  • 让eclipse支持velocity语法高亮,Help->Install New Software...在URL内直接添加veloeclipse插件本地镜像 http://192.168.6.192/veloeclipse/ 安装veloeclipse插件即可。
  • views目录下创建home.vm并添加如下内容

    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
    <h1>
        Hello world!  
    </h1>
    
    
    <P>  The time on the server is ${serverTime}. </P>
    
    
    </body>
    </html>
    
  • 运行你的项目测试veolocity模板吧。选择当前项目Ctrl+F11启动运行圣诞框,选择Run on Server。在这里我们使用默认的VMWare vFabric tc Server Developer Edition来运行项目。

velocity over

comments powered by Disqus