package org.base.controller; import org.base.pojo.Department; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class DeptController { /** * 映射的url http://localhost:端口號/項目名/test.do * 跳轉到 (prefix)test.jsp(suffix)頁面 */ @RequestMapping("/test") public String test(){ System.out.println("test"); return "test"; } /** * 若是方法無返回值,則跳轉到 testP.jsp頁面,即映射名 * 對於方法中的參數,能夠對應url中的參數自動賦值 * 這裏注意,若是url中的參數和方法定義的不一致,例如,此處的age=1.5,那麼會報400錯誤 */ @RequestMapping("/testP") public void testParam(String name,Integer age){ System.out.println("name:"+name+",age:"+age); } /** * 也能夠顯式聲明url參數 */ @RequestMapping("/testP1") public void testParam1(@RequestParam("name")String name,Integer age){ System.out.println("name:"+name+",age:"+age); } /** * 自動注入bean屬性,注意,url中形式是 departmentId=12,不是 對象.屬性 形式 */ @RequestMapping("/testP2") public void testParam2(Department dept){ System.out.println("dept:"+dept); } /** * ResponseBody註解:此方法返回值直接寫入 response的body中,默認是json * 注意要導入 jackson.jar */ @RequestMapping("/test2") @ResponseBody public Department test2(){ Department dept = new Department(12, "abc", 31, 5); return dept; } //@RequestMapping 註解的方法支持不少參數: HttpServletRequest/Response,HttpSession,Writer // 不支持對象.屬性傳值方法,若是重名,/** * restful 風格的傳參方式 */ @RequestMapping("/test3/{str}") public void test3(@PathVariable("str")String str){ System.out.println("str:"+str); } /** * date 日期類型的注入 */ @RequestMapping("/test4") public void test4(@DateTimeFormat(iso=ISO.DATE)Date d){ if(d!=null) System.out.println( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d)); } }
@ResponseBody 能夠將該方法返回的值 寫入到 response的body中去(String,json,xml)。html
@RequestMapping 也能夠用於整個類java
@Controller @RequestMapping("/emp/*") public class EmpController { @RequestMapping("test1") // 這裏匹配的url就是/emp/test1.do public void test1(String s){ System.out.println("s:"+s); } }
spring 配置文件web
<?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:tx=" http://www.springframework.org/schema/tx " 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:mvc=" http://www.springframework.org/schema/mvc " xmlns:p=" http://www.springframework.org/schema/p " xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:config/jdbc.properties" /> <context:component-scan base-package="org.base" /> <!-- 開啓基於註解的mvc配置 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <!-- 解決"IE下返回json提示下載"問題 --> <property name="supportedMediaTypes" value="text/plain;charset=UTF-8"></property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp"></bean> </beans>
web.xmlspring
<?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> <!-- spring mvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
參考:Spring MVC 框架搭建及詳解 json
Spring mvc 返回JSON 在IE 下提示下載 解決辦法 spring-mvc