SpringMvc initial configuration

spring-aop.jar
spring-bean.jar
spring-context.jar
spring-core.jar
spring-web.jar
spring-webmvc.jar
commons-logging.jar

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:aop="http://www.springframework.org/schema/aop"
       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.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/context
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/aop " > 

    <-! scanning annotated package -> 
    < context : Scan-Component Base-Package = "Handler" > </ context: Component-Scan > 

    ! <- configuration view resolver (the InternalResourceViewResolver)   -> 
    < the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver " > 
        < Property name =" prefix " value =" / views / " > </ Property > 
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
    <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>
SpringMVCHandler
package handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//接口/类    注解   配置
@Controller
@RequestMapping(value = "handler") //映射
public class SpringMVCHandler {

    // @RequestMapping(value = "welcome")//映射
    // public String welcome() {
    //     return "success";//  views / success.jsp, default jump mode request forwarded
     // } 
    @RequestMapping (value = "available for purchase", Method = RequestMethod.POST, the params = { "name = ZS", "Age! = 23 is", " ! height "}) // mapping 
    public String available for purchase () {
         return " Success "; //   views / success.jsp, default jump mode request forwarded 
    } 


    @RequestMapping (value =" welcome2 ", headers = {" the Accept = text / HTML, file application / XHTML + XML, file application / XML; Q = 0.9, * / *; Q = 0.8 "," the Accept-Encoding = the gzip, the deflate " })
     public String welcome2 () {
         return " Success " ; //   views / Success.jsp, default jump mode request forwarded 
    } 


    @RequestMapping (value= "Welcome3 / ** / Test" )
     public String welcome3 () {
         return "Success"; //   views / success.jsp, default jump mode request forwarded 
    } 

    @RequestMapping (value = "welcome4 / A C? / Test " )
     public String welcome4 () {
         return " Success "; //   views / success.jsp, default jump mode request forwarded 
    } 

    @RequestMapping (value =" welcome5 / {name} " )
     public String welcome5 ( @PathVariable ( "name" ) String name) { 
        System.out.println (name); 

        return "Success";//   views / success.jsp, use the default request forwarded jump method
    }
}

 index.jsp

<%@ 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">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head> 
< Body > 

<-! If web.xml configuration is 
      <-Mapping the servlet> 
              <the servlet-name> Spring's DispatcherServlet </ the servlet-name> 
              <URL-pattern> .action </ URL-pattern> 
      </ servlet- mapping> 

    <% - <A the href = "User / welcome.action"> First springmvc - </a> available for purchase processing referred springmvc map to find @RuestMapping -%> 
    <a href="user/welcome.action"> first springmvc - welcome </a> referred springmvc process to find @RuestMapping map 
    <a href="xxx/welcome"> first springmvc - welcome </a> referred servlet-parttern process to find url / @ WebServlet () 
 -> 
<% - <a href="handler/welcome3/xyz/abcz/asb/test">33333333get - welcome</a>--%>
<%--<br/>--%>
<%--<a href="handler/welcome4/abc/test">4444444get - welcome</a>--%>
<%--<br/>--%>
<%--<a href="handler/welcome5/zs">555welcome</a>--%>

<form action="handler/welcome" method="post">
    name:<input name="name"><br/>
    age:<input name="age">
    height:<input name="height2">
    <input type="submit" value="post">
</form>

<a href="handler/welcome">first springmvc - welcome</a>

</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>welcome success</h1>

</body>
</html>

 

Guess you like

Origin www.cnblogs.com/kikyoqiang/p/11876551.html