SPRINGMVC Integrated Spring

Source: Internet
Author: User

1,web.xml

<?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>WebProject</Display-name>    <welcome-file-list>        <Welcome-file>index.jsp</Welcome-file>    </welcome-file-list>    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:applicationContext.xml</Param-value>    </Context-param>    <servlet>        <Servlet-name>Springmvc</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>Springmvc</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping>        <Listener>         <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener></Web-app>

2 Applicationcontext.xml

<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/beans http://www.springframework.org/schema/beans/ Spring-beans-4.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/con Text/spring-context-4.1.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/ Mvc/spring-mvc-4.1.xsd ">    <!--Automatic Scanning -    <Context:component-scanBase-package= "Com.liu">        <!--Java Class (Controller) that skips @Controller annotations while scanning -        <Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />    </Context:component-scan></Beans>

3,controller


@Controller
@RequestMapping ("/TC")
public class TestController {

@Autowired
Testservice Testservice;

@RequestMapping ("TM")
Public String Test (Modelmap map) {
System.out.println ("Hello World");
return "Test";
}

@RequestMapping ("Spring")
Public String Spring (Modelmap map) {
System.out.println ("Hello Spring");
return Testservice.getview ();
}
}

4 Service

@Service  Public class Implements testservice{    @Override    public  String GetView () {        return ' Spring ' ;    }}

5jsp

test.jsp

< Body > < H2 > Hello world! Test</h2><href= "Spring"> Spring</a></body>

spring.jsp

< Body > < H2 > Hello spring! </ H2 > </ Body >

6 Testing

<?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>WebProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

SPRINGMVC Integrated Spring

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.