Still Silicon Valley SPRINGMVC code note springmvc_3

Source: Internet
Author: User

SPRINGMVC Running Flowchart:



Project structure:



Home index.jsp Code:

<%@ 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" >

Service

Package Com.atguigu.springmvc;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Service; @Servicepublic class UserService {//cannot inject controller layer//@Autowired//private HelloWorld helloworld;public UserService () {System.out.println ("UserService Constructor ...");}

Handler Control layer:

Package Com.atguigu.springmvc;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;@ Controllerpublic class HelloWorld {@Autowiredprivate userservice userservice;public HelloWorld () {System.out.println ( "HelloWorld Constructor ..."); @RequestMapping ("/helloworld") public String Hello () {System.out.println ("success"); System.out.println (UserService); return "Success";}}

Spring configuration file Bean.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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd "><context:component-scan base-package=" Com.atguigu.springmvc "><context: Exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/><context: Exclude-filter type= "Annotation" expression= "Org.springframework.web.bind.annotation.ControllerAdvice"/></ context:component-scan><!--Configure data sources, integrate other frameworks, transactions, and more. --></beans>

SPRINGMVC configuration file Springmvc.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: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-4.0.xsdhttp://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-4.0.xsd "><!--do I need to do spring integration SPRINGMVC? Do you need to join again? Spring's IOC container? Do I need to configure the Contextloaderlistener. 1 in the Web. xml file to start the Spring IOC container? Required: Typically, similar to data sources, transactions, and other frameworks for consolidation are placed in the spring configuration file (not in the SPRINGMVC configuration file). In fact, there are Service and Dao in the IOC container corresponding to the spring configuration file. 2. Not required: Both are placed in the SPRINGMVC configuration file. It is also possible to divide multiple spring profiles and then import other configuration files using an Import node--><!--problem: If Spring's IOC container and SPRINGMVC's IOC container scan packages have coincidentsection, it will cause some beans to be created 2 times. Resolution: 1. The package scanned by the Spring IOC container and the SPRINGMVC of the IOC container do not have a coincident portion. 2. Use Exclude-filter and Include-filter subnodes to specify annotations that can only be scanned--><!--the beans in the Springmvc IOC container can refer to the beans in the Spring IOC container. Return to it? Conversely, it is not. Beans in the Spring IOC container cannot refer to beans in the Springmvc IOC container!--><context:component-scan base-package= " Com.atguigu.springmvc "use-default-filters=" false "><context:include-filter type=" annotation "expression=" Org.springframework.stereotype.Controller "/><context:include-filter type=" annotation "expression=" Org.springframework.web.bind.annotation.ControllerAdvice "/></context:component-scan><!--Configuration View Resolver-- ><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= " Prefix "value="/web-inf/views/"></property><property name=" suffix "value=". JSP ></property> </bean><mvc:default-servlet-handler/><mvc:annotation-driven></mvc:annotation-driven> </beans>

Xml:

<?xml version= "1.0" encoding= "UTF-8"? ><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/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>springmvc_3</ Display-name> <!--configuration start the Spring IOC container Listener--><!--needed for Contextloaderlistener--><context-pa ram><param-name>contextconfiglocation</param-name><param-value>classpath:beans.xml</ param-value></context-param><!--bootstraps The root Web application context before servlet initialization- -><listener><listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class></listener><servlet><servlet-name>springdispatcherservlet</servlet-name ><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param ><param-name>contextconfiglocation</param-name><param-value>classpath:springmvc.xml</ param-value></init-param><load-on-startup>1</load-on-startup></servlet>< servlet-mapping><servlet-name>springdispatcherservlet</servlet-name><url-pattern>/</ Url-pattern></servlet-mapping></web-app>


Still Silicon Valley SPRINGMVC code note springmvc_3

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.