Build springmvc Project

Source: Internet
Author: User

1. Use maven2 to generate a WEB Project

In the path of the project to be created, open CMD and enter the command:

MVN archetype: Create-dgroupid = com. Sw. webwork-dartifactid = webwork-dversion = 1.0-darchetypeartifactid = Maven-Archetype-webapp

Where:

Archetype: Create indicates that a project structure is to be generated;

-Dgroupid = com. Sw. webwork: defines the groupid In the POM file;

-Dartifactid = webwork: defines the project name;

-Dversion = 1.0: the version number is defined;

-Darchetypeartifactid = Maven-Archetype-webapp indicates that the project type to be generated is a web project, which is not modified.

After running this command, the system will generate a folder named webwork in the current directory, which contains a SRC folder and a pom. xml file.

For more information about groupid and artifactid, see 《Get started with Maven-Concepts and Instances: Http://www.blogjava.net/jiangshachina/archive/2006/09/01/67080.html

2. Change the folder structure to a standard project layout.

The generated folder structure is as follows:

This is not a Maven standard folder structure. For the standard Maven project layout, see development.

Now we need to add some missing folders according to the standard Maven project layout:

1. Add the resources folder under src/main to store configuration files;

2. Add the test folder under SRC to store test-related files;

3. Add a Java folder under src/test to store the test Java source file;

4. Add the resources folder under src/test to store the configuration files for testing;

In this case, the folder structure is changed to the following:

3. Add the dependent jar package

To rely on the jar package groupid, artifactid and version number, you can go to the http://www.mvnrepository.com/query.

4. Add the jetty plug-in for the project

Now we want to verify whether the maven-generated web project can run. We can run this web project through an embedded Web Container named Jetty to verify the function.

We need to add the jetty plug-in for this project. In Pom. XML, add the following configuration in the <build> tag:

<Plugins>

<! -- Initializing ing the jetty plugin -->

<Plugin>

<Groupid> org. mortbay. jetty </groupid>

<Artifactid> Maven-jetty-plugin </artifactid>

</Plugin>

</Plugins>

Set the JDK version for Maven compile to avoid some problems caused by compiling by jdk1.3 by default (http://www.gbsou.com/2009/12/09/1697.html ):

<Plugin>
<Groupid> org. Apache. Maven. plugins </groupid>
<Artifactid> Maven-compiler-plugin </artifactid>
<Configuration>
<Sources> 1.5 </source>
<Target> 1.5 </Target>
</Configuration>
</Plugin>

Next, run the "MVN jetty: Run" command in the webwork root directory. After Jetty is started, enter the http: // localhost: 8080/webwork/link in the browser, we can access the first hello World page.

5. Add spring MVC features

Define dispatcherservlet in Web. XML, which is the front-end controller of spring MVC:

<! -- Spring MVC dispatcherservlet -->
<Servlet>
<Servlet-Name> webwork </servlet-Name>
<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

Add Servlet Mapping to specify the requests to be processed by the dispatcherservlet that will be handed over to spring:

<! -- Dispatcher Servlet Mapping -->
<Servlet-mapping>
<Servlet-Name> webwork </servlet-Name>
<URL-pattern> *. htm </url-pattern>
</Servlet-mapping>

Then add Chinese support:

<! -- To support Chinese GBK encoding -->
<Filter>
<Filter-Name> charactersetencoding filter </filter-Name>
<Filter-class> org. springframework. Web. Filter. characterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> GBK </param-value>
</Init-param>
<Init-param>
<Param-Name> forceencoding </param-Name>
<Param-value> false </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> charactersetencoding filter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

Add context-Param and contextloaderlistener to listen to the custom bean configuration file:

<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
/WEB-INF/*-beans. xml
</Param-value>
</Context-param>

<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>


After configuring web. XML, we also need to add a webwork-servlet.xml file with the following content:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public
"-// Spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>
<Beans>
<! -- Autodetect pojos labeled with the @ requestmapping annotation -->
<Bean class = "org. springframework. Web. servlet. MVC. annotation. defaultannotationhandlermapping"/>

<! -- Autodetect Methods labeled with the @ requestmapping annotation -->
<Bean class = "org. springframework. Web. servlet. MVC. annotation. annotationmethodhandleradapter"/>

<! -- Autodetect pojos labeled with the @ controller annotation -->
<Context: component-scan base-package = "com. SW"/>

<! -- Aggreger that sets up a shared velocityengine for velocity views -->
<Bean id = "velocityconfigurer"
Class = "org. springframework. Web. servlet. View. Velocity. velocitycycler">
<Property name = "velocityproperties">
<Props>
<Prop key = "input. encoding"> GBK </prop>
<Prop key = "output. encoding"> GBK </prop>
</Props>
</Property>
<Property name = "resourceloaderpath" value = "WEB-INF/views/"/>
</Bean>
<! -- Simple viewresolver for velocity, appending ". VM" to logical view names -->
<Bean id = "viewresolver"
Class = "org. springframework. Web. servlet. View. Velocity. velocityviewresolver">
<Property name = "contenttype" value = "text/html; charset = GBK"/>
<Property name = "suffix" value = ". VM"/>
</Bean>
</Beans>

The first three are used to support spring MVC's Annotation configuration, and the last two are used to support velocity configuration.

Add the jar package dependency of springmvc to <dependencies> in POM. xml:

<Dependency>

<Groupid> org. springframework </groupid>

<Artifactid> spring-webmvc </artifactid>

<Version> 2.5.6 </version>

</Dependency>

Then add the jar package dependency of velocity:

<Dependency>
<Groupid> velocity </groupid>
<Artifactid> velocity-dep </artifactid>
<Version> 1.4 </version>
</Dependency>


Then, use MVN jetty: Run to check whether the Web server can be started successfully.


6. Write Vm and Controller


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.