The project structure is as follows:
Spring Configuration
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmln S:mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <context:component-scan base-package=" com.* "/><!--It's best to build a parent package to scan--><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "prefix" >< Value>/web-inf/pages</value></property><property name= "suffix" ><value>.jsp</value></property></bean></beans>
Web. XML configuration
<web-app> <display-name>spring MVC Application</display-name > <servlet> < servlet-name>mvc-dispatcher</servlet-name> < servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name >contextconfiglocation</param-namE> <param-value>classpath*:mvc-dispatcher-servlet.xml </param-value> </context-param> < Listener> <listener-class> org.springframework.web.context.contextloaderlistener</listener-class> </ Listener> </web-app>
There is a class in spring that reads the properties file (this is not related to this project, mention it)
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name= " Locations "><list><value>classpath:config.properties</value></list></property> </bean>
Page under the hello.jsp:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Controller class:
Package Com.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpServletResponse ; Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Controller; Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.servlet.modelandview;import Com.service.Myservice; @Controller @requestmapping ("/spring") public class mycontroller{@Autowiredprivate MyService My, @RequestMapping ("/add") public Modelandview Add ( HttpServletRequest request, HttpServletResponse response) {SYSTEM.OUT.PRINTLN ("----add----");
String xx=request.getparameter ("name"); System.out.println (XX); String name= "123"; My.add (name); return new Modelandview ("/hello", "message", "add"); } @RequestMapping ("/del") public Modelandview del (httpservletrequest request, httpservletresponse response) { System.out.println ("----del----"); return new Modelandview ("/hello", "message", "Del"); }}User class:
Package Com.model;import Org.springframework.context.annotation.scope;import org.springframework.stereotype.Component; @Component @scope ("prototype")//Set into multiple public class User {private int id=20; Private String name= "liming";p ublic int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Proxy class:
Package Com.proxy;import Org.springframework.stereotype.Component, @Componentpublic class Myproxy {public void Add () { System.out.println ("myproxy");}}
Service Interface:
Package Com.service;public interface MyService {public void Add (String name);
Service Implementation class:
Package Com.service.impl;import Com.model.user;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.stereotype.service;import Com.proxy.myproxy;import com.service.myservice;@ Servicepublic class Myserviceimpl implements myservice{@Autowiredprivate myproxy myproxy; @Autowiredprivate User user;@ overridepublic void Add (String name) {System.out.println ("This is Serviceimpl:" +name); System.out.println (User.getname () + "This year" +user.getid () + "years old"); Myproxy.add ();}}
After running with Tomcat, enter http://localhost:8080/spring/spring/add?name= "XX"
The console results are:
----Add----"XX" This is serviceimpl:123liming 20 years old this year myproxy
and displayed on the page
This is my JSP page. This call method is the add
Spring Full Annotated Project