1. spring MVC-annotation(注解)的配置文件ApplicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.happy.*"></context:component-scan>
</beans>
01.spring MVC最基本的注解之零散參數自動裝配
@Controller
@RequestMapping("/hr")
public class MyController {
@RequestMapping("/hello.do")
public String show(String name,Model model){
System.out.println("=="+name+"==");
model.addAttribute("msg",name+"展示頁面");
return "happy";
}
}
其中,方法中的參數與界面表單的name屬性並且和實體類中的字段name保持一直("三者合一"),Model類型代替了ModelAndView的用法去裝載數據然後直接return到jsp界面,
如果直接像圖中返回happy那樣就需要在配置文件中添加一個視圖解析器
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
如果界面中的屬性不一致,則需用到注解@RequestParam來進行指明
@RequestMapping(value="/list.do",method=RequestMethod.POST)
public String list(Model model,@RequestParam(value="uname",required=false) String name){
System.out.println("=="+name);
return "happy";
}
02.spring MVC注解之參數以對象、作用域、map、泛型作為傳輸數據
//裝配對象類型
@RequestMapping(value="/list.do",method=RequestMethod.POST)
public String list(Model model,UserInfo info){
System.out.println("===="+info.getUname()+"\t地址"+info.getRadd().getAdd()+"\t圖書1"+info.getBooklist().get(0).getBookname());
model.addAttribute("uname", info.getUname());
return "index";
}
03.獲取請求的地址欄中的屬性參數值
@Controller
public class HandleReturn {
/*
* 獲取地址欄中的屬性值
*/
@RequestMapping("/{rname}/{age}/first.do")
public String handlereturn(Model model,@PathVariable("rname") String name,@PathVariable int age){
System.out.println(name+"==="+age);
model.addAttribute("name", name);
model.addAttribute("age", age);
return "handle";
}
}
其中,如果地址欄中的屬性名稱與方法參數名不一致,就通過如代碼所示的注解@PathVariable來指明地址欄中的屬性名稱rname與name關系
04.spring MVC注解之返回值void、Object、string
@Controller
public class HandleAjax {
@RequestMapping("/ajax.do")
public void handleAjax(HttpServletResponse response) throws Exception{
//虛擬出一些數據
Map<String, UserInfo> map=new HashMap<String,UserInfo>();
UserInfo u1=new UserInfo();
u1.setAge(12);
u1.setName("恭喜就業");
UserInfo u2=new UserInfo();
u2.setAge(122);
u2.setName("順利就業");
map.put("001",u1);
map.put("001",u2);
//工具 map----json字符串 fastjson
String jsonString = JSON.toJSONString(map);
response.setCharacterEncoding("utf-8");
//響應流
response.getWriter().write(jsonString);
response.getWriter().close();
}
}
/*
* Object返回值類型代替其他類型
*/
@Controller
public class HandleAjaxObject {
@RequestMapping("/num.do")
@ResponseBody
public Object number() {
return 1;
}
@RequestMapping(value = "/nums.do", produces = "text/html;charset=utf-8")
@ResponseBody
public Object numberS() {
return "漢字";
}
// 處理器方法-----UserInfo
@RequestMapping(value = "/third.do")
@ResponseBody
public Object doThird() {
UserInfo info = new UserInfo();
info.setAge(12);
info.setName("Happy");
return info;
}
使用Object作為返回值需要用到注解@ResponseBody來將數據回傳到jsp界面
----js
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.ajax({
url:"nums.do",
success:function(data){ //data指的是從server打印到浏覽器的數據
alert(data)
}
});
});
});
</script>
-----body
<input type="button" id="btn" value="Ajax"/>
使用void返回值返回json數據回傳到jsp界面
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.ajax({
url:"ajax.do",
success:function(data){ //data指的是從server打印到浏覽器的數據
//jsonString jsonObject
//{"001":{"age":122,"name":"順利就業"}}
var result= eval("("+data+")");
$.each(result,function(i,dom){
alert(dom.age)
});
}
});
});
});
</script>
<input type="button" id="btn" value="Ajax"/>
最後就是String類型的了,不過跟void類型的返回值很相似
使用String作為返回值需要用到注解@ResponseBody來支持json數據回傳到jsp界面
/*
* String返回值類型代替其他類型
*/
@Controller
public class HandleAjax {
@RequestMapping("/ajax.do")
public void handleAjax(HttpServletResponse response) throws Exception{
//偽造數據
Map<String, UserInfo> map=new HashMap<String,UserInfo>();
UserInfo u1=new UserInfo();
u1.setAge(12);
u1.setName("恭喜就業");
UserInfo u2=new UserInfo();
u2.setAge(122);
u2.setName("順利就業");
map.put("001",u1);
map.put("001",u2);
//工具 map----json字符串 fastjson
String jsonString = JSON.toJSONString(map);
response.setCharacterEncoding("utf-8");
return jsonString ;
}
05.重定向到另一個方法去
/*
* 轉發與重定向
* 重定向到另一個方法
*/
@Controller
public class Dispatchreturn {
/*
* 重定向到另一個方法dsipatch.do
*/
@RequestMapping(value="add.do")
public String AddAllInfo(){
return "redirect:dolist.do";
}
@RequestMapping(value="dolist.do")
public String doList(){
return "redirect:/list.jsp";
}
}
注意:"/"可寫可不寫