springmvc简单使用
1.创建一个web工程
2.导入所需的jar包
3.配置文件中的DispatcherServlet,由于已经安装了插件,所以只需通过快捷键alt+/即可
<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.创建一个配置文件里classpath后的xml文件(插件可直接插入此类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: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.创建一个包扫描下的包,通过注释@进行操作
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.接受参数,如果接收的是单独的参数,方法的参数列表对应的名字必须与传过来的名字一致,如果为对象,则必须与实体类属
7.Controller如何进行重定向跳转加入redirect:即可
8.解决静态资源的映射(springmvc.xml加入即可)
<mvc:default-servlet-handler/>