idea还不会用,也不懂用maven……所以Myeclipse解决
尽量详细去写吧~
导包-建web工程-要包含web.xml文件
Dispatcherservlet
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
它在spring加载的时候就被创建了
【init-param】配置dispatcherservlet的初始化参数,用来配置springmvc配置文件的位置和名称
【url-pattern】你想要它应答什么请求,【/】表示应答所有请求
【classpath】写spring配置文件名,默认你该把它放在src下
模板(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" id="WebApp_ID" version="2.5">
<display-name>SpringMVC</display-name>
<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>
目录结构
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd ">
<!-- 自动扫描的包 -->
<context:component-scan base-package="com.yiki.Handle"></context:component-scan>
<!-- 配置视图解析器:如何把handler方法解析为实际物理视图 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/view/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
控制层
package com.yiki.Handle;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller//标识为请求处理器
public class HelloWorld {
/*返回值会通过物理解析器解析为物理视图--》ResourceViewResolver
* 然后转发
* 前缀+返回值+后缀====》转换物理视图
* /WEB-INF/view/+success+.jsp
* */
@RequestMapping("/helloworld")//映射请求,是一个url
public String hello(){
System.out.println("helloworld");
return "success";
}
}
@RequesMapping
我觉得用于处理各种超链接或者表单请求,而这个注解的参数拿来过了符合条件的请求
该注解可以在控制器类的级别和/或其中的方法的级别上使用,并且可以将多个映射请求到同一个方法上
修饰类的时候,全局加上路径前缀,加方法,就是直接路径了。具体看下面代码
/* JSP方请求应写:springmvc/request */ @RequestMapping("/springmvc") @Controller public class RequstMapping { private static final String SUCCESS = "success"; @RequestMapping("/request") public String request() { System.out.println("request"); return SUCCESS; }
属性
注意:超链接是个get请求
RequestMapping注解有六个属性,
value:
指定请求的实际地址,指定的地址可以是URI Template ;
method:
指定请求的method类型, GET、POST、PUT、DELETE等;
<a href="springmvc/testMethod">springmvc/testMethod(接收不到)</a><br> <form action="springmvc/testMethod" method="POST"> <input type="submit" value="testMethod"> </form>
@RequestMapping(value = "/testMethod", method = RequestMethod.POST) public String testMethod() { System.out.println("Method"); return SUCCESS; }
consumes:
指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces:
指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params:
p1:必须包含名字为p1的请求参数
!p1:必须不能包含名字为p1的请求参数
{"p1=value1","p2"}请求必须包含为p1和p2且p1必须为vlalue1
<a href="springmvc/testParams?name=yiki&age=1">testParams</a><br>
@RequestMapping(value = "/testParams", params={"name","age!=10"}) public String testParams() { System.out.println("testParams"); return SUCCESS; }
headers:
指定request中必须包含某些指定的header值,才能让该方法处理请求
ant风格
<a href="springmvc/testAnt/yiki/ant">/testAnt/*/ant</a><br>
@RequestMapping("/testAnt/*/ant")//*里写什么都没无所谓 public String testAnt() { System.out.println("testAnt"); return SUCCESS;
@PathVariable
形如
@RequestMapping("/delete/{id}") public String delete(@PathVariable("id") Integer id) { UserDao.delete(id); return "user/list.action"; }
<a href="springmvc/testVariable/1">/testAnt/*/ant</a>