Integrated springmvc

Build a controller of the package, write a TestController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("con")
public class TestController {

    public TestController(){
        System.out.println("123123123");
    }
    
    @RequestMapping("test")
    @ResponseBody
    public String testmvc(){
        System.out.println("testmvc");
        return "testmvc";
    }
    
}

Write constructors convenient scanning to see if

 

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:p="http://www.springframework.org/schema/p"
    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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">  <-! Configuration Controller Scan ->
    
    <context: Component Base-Package-Scan = "Controller" />
 <-! drive configuration annotation ->  <MVC: Annotation-Driven /> 
 <-! configuration view resolver -> ! <- <the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> prefix <property name = "prefix" value = "/ WEB-INF / jsp /" /> suffix <property name = "suffix" value = ". jsp "/> </ the bean> -> </ Beans>

springmvc.xml need to be configured to scan the packet controller, prefixes and suffixes in the jump action is jsp prefix and suffix

 

Add springmvc configuration in the 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_2_5.xsd" version="2.5">
  <display-name>smh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <!-- 配置spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 配置监听器加载spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置过滤器,解决post的乱码问题 -->
    <filter>
        <filter-name>encoding</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
    <filter>
      <filter-name>openSessionInView</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
    <filter-mapping>
      <filter-name>openSessionInView</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

    <!-- 配置SpringMVC -->
    <servlet>
        <the servlet-name> Boot-CRM </ the servlet-name> 
        <the servlet-class> org.springframework.web.servlet.DispatcherServlet </ the servlet-class> 
        <the init-param> 
            <param-name> the contextConfigLocation </ param-name >     <param-value> CLASSPATH: springmvc.xml </ param-value> 
        </ the init-param> 
        <! - springmvc configuration when started, must be an integer parameter -> 
        <! - if 0 or greater , the promoter is activated as the container springMVC -> 
        <! - if less than 0, the first time a request comes in to start -> 
        <Load-oN-startup>. 1 </ Load-oN-startup> 
    < / the servlet> 
    <-Mapping the servlet> 
        <the servlet-name> Boot-CRM </ the servlet-name> 
        <-! all requests are entered springMVC ->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
        
 
</web-app>

 

Access path: HTTP: // localhost: 8080 / art_test / CON / test.action (remember to add the project name)

 

 

Guess you like

Origin www.cnblogs.com/withbear/p/11850106.html