JAVA-SPRINGMVC based on annotation mode the first application

Source: Internet
Author: User

Project file Structure

1. Web. XML configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"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>Test</Display-name>  <!--Configuring the Springmvc front-end configuration -  <servlet>  <Servlet-name>Springmvc</Servlet-name>  <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>  <Init-param>  <Param-name>Contextconfiglocaltion</Param-name>  <Param-value>/web-inf/springmvc-servlet.xml</Param-value>  </Init-param>  <Load-on-startup>1</Load-on-startup>  </servlet>    <servlet-mapping>  <Servlet-name>Springmvc</Servlet-name>  <Url-pattern>/</Url-pattern>  </servlet-mapping>      <welcome-file-list>    <Welcome-file>index.jsp</Welcome-file>  </welcome-file-list></Web-app>

2.springmvc-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.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-3.1.xsd " >      <!--Configuring the annotation scan package -    <Context:component-scanBase-package= "Com.luo.test"/>  <!--register a beans to support request handling methods for Zhujie-based controllers -    <Mvc:annotation-driven></Mvc:annotation-driven>    <mvc:resources Location= "/css/"Mapping= "/css/**"/>    <mvc:resources Location="/"Mapping= "/*.html"/>     <!--Configure mapping prefixes and suffixes for front-end pages -     <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">     <!--configuration prefix, view path -        < Propertyname= "prefix"value= "/web-inf/view/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean></Beans>

3.controller

 Packagecom.luo.test;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHellocontroller {Private Static FinalLog Logger=logfactory.getlog (Hellocontroller.class); @RequestMapping (Value= "/hello")     PublicString Hello () {Logger.info ("Hello called"); return"Hello"; }}

4.view

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body><H1>Hello</H1></Body></HTML>

5. Running Results

Annotation mode principle: By scanning the note file to find the controller to find the corresponding view

JAVA-SPRINGMVC based on annotation mode the first application

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.