springmvc simple to use

1. Create a web project

2. Import required jar package

 

3. profile DispatcherServlet, because the plug has been installed, so only through the shortcut alt + / to

 

 

<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-annotation.xml</param-value>
         </init-param>
     </servlet>
     <servlet-mapping>
         <servlet-name>springMVC</servlet-name>
         <url-pattern>*.do</url-pattern>
     </servlet-mapping>

4. Create a configuration file xml file (such plug directly into xml), and scans the package after the classpath, the drive and open the note written parser mentoring

<?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: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.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <context:component-scan base-package="com.zhiyou.zt.annotation"></context:component-scan>
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

5. Create a packet at the packet scan, operated by @ Notes

package com.zhiyou.zt.annotation;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou.zt.bean.Users;

@Controller
@RequestMapping("/user")
public class my {
    
        }


6. accept parameters, if the received parameter is a separate parameter list corresponding to the name of the method must pass over the same name, if an object, you must belong to the entity class

 

 

 

 

 

 7.Controller how to redirect Jump join redirect: to

 

 8. solve static resource mapping (springmvc.xml can be added)

<mvc:default-servlet-handler/>

 

Guess you like

Origin www.cnblogs.com/1556553526qq-com/p/11456355.html