Springmvc搭建簡單測試demo
建立第一個簡單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
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
<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() {