导入jar包
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INT/classes/spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
写TestController.java和相应的注解
package test.controller;
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
public class TestController {
@RequestMapping("/start/{name}")
public String start(@PathVariable("name") String name){
System.out.println(name);
return "start";
}
}*/
@Controller
public class TestController {
@RequestMapping(value="/start", method=RequestMethod.GET)
public String start(){
return "start_get";
}
@RequestMapping(value="/start", method=RequestMethod.POST)
public String Post_start(){
return "start_post";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
在servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="test.controller"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value= "/WEB-INF/page/" />
<property name="suffix" value= ".jsp" />
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
配置视图解析器:
启动tomcat测试 成功!
项目目录结构
注:在类上加上@RequestMapping(/test)
对应action test/start.do action名 action方法
则以上访问路径应该是http://localhost:8080/@MVC/test/start.do
进阶版
从路径里取参数,如http://localhost:8080/@MVC/test/start/zhangsan.do从中取值zhangsan
test: action名 start: action的方法名 zhangsan: 传递的参数名
修改TestControl.java
启动tomcat测试 成功!
注:如果是用javac编译(默认用release,而myeclipse默认是debug),则需要在@PathVariable后面加(”name”)
如果需要从路径取多个参数则多添加几个@PathVariable 和String arg
然后URL地址为
同一个请求方法start根据请求是post还是get来决定使用哪个方法
通过初始化绑定器转换日期等的参数的类型
还可以直接使用out
还可以在model里定义各种视图
直接把表单的数据传入给user,不需要设置
重定向,防止重复提交
后端验证:
导入jar包