创建第一个简单springmvc步骤。
(一)首先配置好jdk、tomcat。
(二)导入jar包
(三)添加配置文件:springmvc.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自定扫描的包 -->
<context:component-scanbase-package="com.atguigu.springmvc"></context:component-scan>
<!-- 配置视图解析器: 如何把 handler方法返回值解析为实际的物理视图 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/views/"></property>
<propertyname="suffix"value=".jsp"></property>
</bean>
<!-- 配置视图 BeanNameViewResolver解析器:使用视图的名字来解析视图 -->
<!-- 通过 order 属性来定义视图解析器的优先级, order值越小优先级越高 -->
<beanclass="org.springframework.web.servlet.view.BeanNameViewResolver">
<propertyname="order"value="100"></property>
</bean>
<!-- 配置国际化资源文件 -->
<beanid="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<propertyname="basename"value="i18n"></property>
</bean>
<!-- 配置直接转发的页面 -->
<!-- 可以直接相应转发的页面, 而无需再经过 Handler 的方法. -->
<mvc:view-controllerpath="/success"view-name="success"/>
<!-- 在实际开发中通常都需配置 mvc:annotation-driven标签 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
(四)配置web文档
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
<display-name>SpringMVC_Demo0621a</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>
dispatcherServlet
</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>-->
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter:可以把 POST请求转为 DELETE或 POST请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 指定 dispatcherServlet处理所有请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(五)创建controller
package com.atguigu.springmvc.handler;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.ResponseBody;
/**
*
*@author HH
*@date 2017-6-21
*@version jdk 1.7.0
*@function springmvc测试1
*/
@Controller
@RequestMapping("/HelloWork")
public class HelloWork {
@ResponseBody
@RequestMapping(value="/helloword",method=RequestMethod.GET)
public Stringhello(){
//在控制台后边打印出下边这句话,并跳转到success页面,
System.out.println("hello,create aworld!");
return "success";
}
@RequestMapping("/helloword2")
@ResponseBody
//在控制台后边打印出下边这句话,并输出success
publicString sayHello(){
System.out.println("sayhello to springmvc");
return"success";
}
//06月22日方法
//必须使用post提交方法
}
Springmvc搭建框架初步(完成以上的springmvc.xml配置和web.xml配置后)
(一)springmvc通过get方法跳转。
@RequestMapping(value="testMethod",method=RequestMethod.GET)
public String testMethod(){
System.out.println("this is 06月22日测试");
return"success";
}
在jsp中:
<ahref="<%=basePath%>SpringMVC6_22/testMethod">06月22日测试</a>
(二)通过params传参,把age判断不为10才能正常跳转
@RequestMapping(value="testMethod02",params={"username","age!=10"},method=RequestMethod.GET)
public String testMethod02(){
System.out.println("this is 06月22日testMethod02测试");
return"success";
}
在jsp中:
<ahref="<%=basePath%>SpringMVC6_22/testMethod02?username=aaa&&age=12">06月22日testparam测试</a>
(三)在通过使用占位符。/*/表示上一层任意的字符,也可以匹配到。
/* //*匹配任何字符,但是必须是该路径下,requestmapping层必须要写上才能找到*/
/* 2个*表示任意层*/
@RequestMapping("/*/testAntString03")
//只能是一层,上一层是任意的也可以
public StringtestAntString03(){
System.out.println("testAntString03");
return"success";
}
在jsp中:
<ahref="<%=basePath%>SpringMVC6_22/year/testAntString03">通过*访问</a>
(四)可以是任意多层。
@RequestMapping("/**/testAntString04")
//可以是多层任意的
public StringtestAntString04(){
System.out.println("testAntString04");
return"success";
}
在jsp中:(year/fdf/fsdfd为任意多层)
<ahref="<%=basePath%>SpringMVC6_22/year/fdf/fsdfd/testAntString04">通过多层**访问</a>
(五)通过PathVariable传参
@RequestMapping("/testAntString05/{id}/{name}")
//可以是多层任意的
public String testAntString05(@PathVariable("id") Integerid,@PathVariable("name") Stringname){
System.out.println("testAntString05的id:"+id+",name:"+name);
return"success";
}
在jsp中:
<ahref="<%=basePath%>SpringMVC6_22/testAntString05/25/popo">通过参数去访问</a>
(六)通过rest,_method参数传参
@RequestMapping(value="order/{id}",method=RequestMethod.DELETE)
//可以是多层任意的
public StringtestAntString06Delete(@PathVariable("id")Integerid){
System.out.println("testAntString06Delete的id:"+id);
return"success";
}
//查询用户
@RequestMapping(value="/order/{id}",method=RequestMethod.GET)
//可以是多层任意的
public StringtestAntString06GET(@PathVariable("id")Integerid){
System.out.println("查询用户的id:"+id);
return"success";
}
//post 询无id
//添加户
@RequestMapping(value="/order/{id}",method=RequestMethod.PUT)
//可以是多层任意的
public StringtestAntString06PUT(@PathVariable("id")Integerid){
System.out.println("testAntString06PUT添加用户的id:"+id);
//HiddenHttpMethodFilter?
return"success";
}
在jsp中:
<hr>
<h1>通过rest删除添加用户</h1>
<formaction="<%=basePath%>SpringMVC6_22/order/22"method="post">
<inputtype="hidden"name="_method"value="PUT"/>
<inputtype="submit"value="添加用户id PUT">
</form>
<formaction="<%=basePath%>SpringMVC6_22/order/22"method="post">
<inputtype="hidden"name="_method"value="DELETE"/>
<inputtype="submit"value="删除用户">
</form>
<formaction="<%=basePath%>SpringMVC6_22/order/22"method="post">
<inputtype="hidden"name="_method"value="GET"/>
<inputtype="submit"value="查找用户">
</form>
(七)通过RequestParam绑定参数。(required=false表示允许无参数)
@RequestMapping(value="/testAfternoon0622Test02",method=RequestMethod.POST)
public StringtestAfternoon0622Test02(@RequestParam("username")Stringusername,@RequestParam(value="age",required=false)Integerage){
System.out.println("this is http请求处理方法签名测试!");
System.out.println("username:"+username+",age:"+age);
returnSUCCESS;
}
在jsp中:
<h1>无参数测试</h1>
<formaction="<%=basePath%>SpringMVC6_22/testAfternoon0622Test02?username=kaka" method="post">
<inputtype="submit"value="绑定无参数参数用户">
</form>
<hr>
(八)通过对象绑定参数
在controller中代码:
@RequestMapping(value="/testAfternoon0622Test",method=RequestMethod.POST)
public StringtestAfternoon0622Test(@RequestParam(value="username",defaultValue="play",required=false)Stringusername,@RequestParam(value="age",defaultValue="20",required=false)Integerage){
System.out.println("this is http请求处理方法签名测试!");
System.out.println("username:"+username+",age:"+age);
returnSUCCESS;
}
@RequestMapping(value="/testPOJO",method=RequestMethod.POST)
public String testPOJO(@ModelAttribute("user")Useruser){
System.out.println("user的员工信息:\n"+user);
returnSUCCESS;
}
创建对象:
package com.atguigu.springmvc.entity;
publicclass User {
private Stringname;
private Stringpassword;
private Stringemail;
private Integerage;
public String getName() {
returnname;
}
publicvoid setName(Stringname) {
this.name =name;
}
public String getPassword() {
returnpassword;
}
publicvoid setPassword(Stringpassword) {
this.password =password;
}
public String getEmail() {
returnemail;
}
publicvoid setEmail(Stringemail) {
this.email =email;
}
public Integer getAge() {
returnage;
}
publicvoid setAge(Integerage) {
this.age =age;
}
@Override
public String toString() {
return"User[name=" +name +", password=" +password +", email="
+ email + ",age=" + age + "]";
}
}
在jsp中:
<hr>
<formaction="<%=basePath%>SpringMVC6_22/testAfternoon0622Test?username=rtrd&age=43" method="post">
<inputtype="submit"value="绑定参数用户">
</form>
<hr>
(九)通过嵌套对象测试练习
创建对象Address,嵌套对象MyUser
package com.atguigu.springmvc.entity;
publicclassAddress {
private Stringline;
private Stringcity;
private Stringprovince;
private MyUsermyUser;
// HiddenHttpMethodFilter
public MyUser getMyUser() {
returnmyUser;
}
publicvoid setMyUser(MyUsermyUser) {
this.myUser =myUser;
}
public String getLine() {
returnline;
}
publicvoid setLine(Stringline) {
this.line =line;
}
public String getCity() {
returncity;
}
publicvoid setCity(Stringcity) {
this.city =city;
}
public String getProvince() {
returnprovince;
}
publicvoid setProvince(Stringprovince) {
this.province =province;
}
@Override
public String toString() {
return"Address [line="+line+", city="+city+", province="
+province+", myUser=" +myUser+"]";
}
}
@RequestMapping(value="/testPOJOnoObject",method=RequestMethod.POST)
public StringtestPOJOnoObject(Addressaddress){
//本类的不需要加上对象名,而如果是其他类的需要写上对象名字
System.out.println("user的员工信息:\n"+address);
System.out.println("address.getMyUser().getName():"+address.getMyUser().getName());
returnSUCCESS;
}
在jsp中:
<h1>POJO测试练习没有对象的</h1>
<formaction="<%=basePath%>SpringMVC6_22/testPOJOnoObject"method="POST">
this is test province:
Username: <input type="text"name="myUser.name"><br>
password:<inputtype="text"name="myUser.password"><br>
email:<inputtype="text"name="myUser.email"><br>
age:<inputtype="text"name="myUser.age"><br>
line: <inputtype="text"name="line"><br>
city:<inputtype="text"name="city"><br>
province:<inputtype="text"name="province"><br>
<inputtype="submit"value="提交POJO用户"><br>
</form>
(十)writer对象使用练习测试
@RequestMapping("/testServletAPI")
publicvoidtestServletAPI(HttpServletRequestrequest,HttpServletResponseresponse
,Writer writer)throwsIOException{
System.out.println(response.toString());
writer.write("<xml>hello world</xml>");
}
在jsp中:
<hr>
<h1>testServletAPI测试练习</h1>
<ahref="<%=basePath%>SpringMVC6_22/testServletAPI">testServletAPI测试</a>
<br>
<hr>
(十一)springmvc的Map对象使用
@RequestMapping("/testMapTest")
public String testMapTest(Useruser,Map<String,Object>map){
map.put("time",new Date());
map.put("user",user);
System.out.println(user);
// user.setEmail("abc@qq.com");
return"result";//跳转这个页面
}
在jsp中
<h1>测试requestSCOPE的map测试练习</h1>
<formaction="<%=basePath%>SpringMVC6_22/testMapTest"method="POST">
this is test province:
Username: <input type="text"name="name"><br>
password:<inputtype="text"name="password"><br>
email:<inputtype="text"name="email"><br>
age:<inputtype="text"name="age"><br>
<inputtype="submit"value="提交"><br>
</form>
跳转到了result.jsp页面,因此,根据配置文档,创建页面。result.jsp页面如下:
<%@pagelanguage="java"import="java.util.*"