• SpringMVC集成Spring


    1,web.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>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

    <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/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.liu">
            <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
            <context:exclude-filter type="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 TestServiceImpl implements TestService{
    
        @Override
        public String getView() {
            return "spring";
        }
    
    }

    5jsp

    test.jsp

    <body>
    <h2>Hello World!Test</h2>
    <a 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>

  • 相关阅读:
    JPA
    XMPP技术之Smack库的自定义消息扩展
    VMVare的窗口自适应
    linux c tcp p2p
    linux 消息队列
    基数排序-LSD
    基数排序-纪念欧某新
    归并排序
    锦标赛排序
    快速排序 之添加复合插入排序和原始序列取中值左pivot
  • 原文地址:https://www.cnblogs.com/imperfectLiu/p/7252778.html
Copyright © 2020-2023  润新知