Springmvc+maven Frame Quick Build Step

Source: Internet
Author: User

Springmvc+maven Integration (Annotation form)

SPRINGMVC is the mainstream of the current framework, coupled with the form of annotations, greatly increased the efficiency of developers, and in the form of annotations, save a lot of XML configuration, so that the code seems to improve a lot of clarity.

The following teaches you how to configure, here to create the project is not introduced, we directly from the configuration of Pom.xml start.

The MAVEN coordinates used by the 1.SpringMVC

  <properties> <spring-version>3.1.0.RELEASE</spring-version> </properties> < dependencies> <!--Spring start--> <dependency> <groupid>org.springframework</group id> <artifactId>spring-web</artifactId> <version>${spring-version}</version> &L t;/dependency> <dependency> <groupId>org.springframework</groupId> <artifactid&gt           ;spring-webmvc</artifactid> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc</artifact id> <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</ar tifactid> <version>${spring-version}</version> </dependency> <dependency> <group Id>org.springframework</groupid> <artifactId>spring-core</artifactId> <version>${spri ng-version}</version> </dependency> <dependency> <groupid>org.springframework</groupid > <artifactId>spring-test</artifactId> <version>${spring-version}</version> &LT;/DEP        endency> <!--Spring start--> <!--log4j start--> <dependency> <groupid>log4j</groupid><artifactid>log4j</artifactid><version>1.2.16</ Version></dependency><dependency><groupid>org.slf4j</groupid><artifactid> slf4j-log4j12</artifactid><version>1.6.1</vErsion></dependency><dependency><groupid>org.slf4j</groupid><artifactid> slf4j-api</artifactid><version>1.6.1</version></dependency> <!--Log4j end--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> < Version>1.2</version> </dependency> <!--jstl Tag Library--<dependency> &LT;GROUPID&G T;taglibs</groupid> <artifactId>standard</artifactId> <version>1.1.2</version> < /dependency> </dependencies>

The spring version chosen here is 3.1.0.RELEASE, and you can make the version according to your needs.

Configuration of the 2.web.xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"4xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"5Id= "webapp_id" version= "3.0" >6     7<context-param>8<param-name>contextConfigLocation</param-name>9<!--Application Context profile--Ten<param-value>classpath:spring/spring-context.xml</param-value> One</context-param> A<listener> -<listener-class>org.springframework.web.context.contextloaderlistener</listener-class> -</listener> the      -<!--Configuring the spring core servlet-- -<servlet> -<servlet-name>daily-collect</servlet-name> +<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> -<init-param> +<description>spring MVC configuration file </description> A<param-name>contextConfigLocation</param-name> at<param-value>classpath:spring/spring-mvc.xml</param-value> -</init-param> -<load-on-startup>1</load-on-startup> -</servlet> -      -<listener> in<listener-class>org.springframework.web.util.log4jconfiglistener</listener-class> -</listener> to<context-param> +<param-name>log4jConfigLocation</param-name> -<param-value>classpath:log4j.properties</param-value> the</context-param> *      $<!--note here that the servlet-name must be the same as the name of the Servlet-name with the spring core configured above, which has a mapping relationship--Panax Notoginseng<!--Url-pattern is configured to/, without a file suffix, which can cause other static files (JS,CSS, etc.) to be inaccessible. If it is a *. Do, it does not affect access to the static file-- -<servlet-mapping> the<servlet-name>daily-collect</servlet-name> +<url-pattern>*. Do</url-pattern> A</servlet-mapping> the      +<!--filer filter is to avoid garbled processing of the requested data character set-- -<filter> $<filter-name>CharacterEncodingFilter</filter-name> $<filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> -<init-param> -<param-name>encoding</param-name> the<param-value>utf-8</param-value> -</init-param>Wuyi</filter> the<!--be consistent with the name above-- -<filter-mapping> Wu<filter-name>CharacterEncodingFilter</filter-name> -<url-pattern>/*</url-pattern> About </filter-mapping> $ <!--Set the default access path-- - <welcome-file-list> - <welcome-file>pages/Index.jsp</welcome-file> - </welcome-file-list> A </web-app>

3.spring-context.xml configuration (Here are some globally used beans or load configuration files, referencing other spring XML).

1<beans xmlns= "Http://www.springframework.org/schema/beans"2xmlns:context= "Http://www.springframework.org/schema/context"3xmlns:p= "http://www.springframework.org/schema/p"4Xmlns:mvc= "Http://www.springframework.org/schema/mvc"5Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"6Xsi:schemalocation= "Http://www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans-3.0.xsd8http//Www.springframework.org/schema/context9http//www.springframework.org/schema/context/spring-context.xsdTenhttp//Www.springframework.org/schema/mvc Onehttp//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> A<!--opening SPRINGMVC annotations-- -<context:annotation-config/> -     the<!--start the package scan feature to register classes with annotations such as @controller, @Service, @repository, @Component and so on as spring beans-- -<context:component-scan base- Package= "Com.code.daily.*.*.controller"/> -      -      +<!--<ImportResource= "Classpath:spring/spring-tx.xml"/>-- -</beans>

4.spring-mvc.xml configuration (mainly on the opening of annotations, need to scan the annotation interception operation) inside a few tags you can directly search Baidu, will have introduced its role.

<beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"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-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><context:annotation-config/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <context:component-scan base- Package= "Com.code.daily.*.*.controller"/> <!--the path resolution of the steering page. Prefix: prefix, suffix: suffix--<bean id= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/ web-inf/pages/"/> <property name=" suffix "value=". jsp "/> </bean></beans>

5.Contorller of code

1  PackageCom.code.daily.service.tester.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 ImportOrg.springframework.web.bind.annotation.RequestMethod;6 ImportOrg.springframework.web.bind.annotation.ResponseBody;7 ImportOrg.springframework.web.servlet.ModelAndView;8 9 @ControllerTen@RequestMapping ("/test") One  Public classFisttestercontrollet { A@RequestMapping (value = "/fisttest", method =requestmethod.get) -      -      Public@ResponseBody Modelandview fisttester () { theModelandview mv =NewModelandview (); -Mv.addobject ("message", "message"); -Mv.setviewname ("Index"); -         returnMV; +     } -}

6.jsp page with header tag imported into JSTL's tag library

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%>    <%@ taglib prefix=" C "uri=" http://java.sun.com/jsp/ Jstl/core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >    ${message}, Fist  page  !!!  </body>

7. Project Structure

8. Mapping of Access Paths Web. XML (Daily-collect) +controller (/test) + (/fisttest) +.do

Http://localhost:8089/daily-collect/test/fistTest.do

9. Common sexual issues

If you have already checked the project, configured this listener in Web. XML, and can find this jar package, but still report the error, then this may be that Maven did not refer to the project at the time of deployment
Solution: Right-click the project to select Properties

Restart the test, you may also encounter some other problems, you can Baidu try to solve, or write the question below, there is time to reply to the answer

Springmvc+maven Frame Quick Build Step

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.