第一个SpringMVC
1.编写web.xml 放置于 WebContent/WEB-INF 下
1 <?xml version ="1.0" encoding ="UTF-8"?> 2 3 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " 6 xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee 7 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> 8 <!-- 配置前端控制器 --> 9 <servlet> 10 <servlet-name>springmvc</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <load-on-startup>1</load-on-startup> 13 <!-- 初始化 --> 14 <init-param> 15 <param-name>contextConfigLocation</param-name> 16 <!-- 绑定springmvc配置文件 --> 17 <param-value>classpath:springmvc.xml</param-value> 18 </init-param> 19 </servlet> 20 21 <!-- 使用前端控制器 --> 22 <servlet-mapping> 23 <servlet-name>springmvc</servlet-name> 24 <!-- 配置拦截 --> 25 <url-pattern>/</url-pattern> 26 </servlet-mapping> 27 28 </web-app>
2.编写springmvc配置文件:
<?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/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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 开启spring注解扫描的包 --> <context:component-scan base-package="user" /> <!-- 开启springmvc默认的器--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 开启mvc的视图转化器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/view/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
3.建立处理类
package user; import java.util.Date; import javax.xml.crypto.Data; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import converter.date_converter; import javabean.userbean; @Controller @RequestMapping("/user") public class user { @RequestMapping(path="/init") public String denglu(userbean user,Data data) { System.out.println("成功了"+user); System.out.println(data); return "init"; } }