JavaWebノート020 SSM統合、受信パラメータ、Restfulスタイル、リダイレクトと転送、ResponseBody null無視
15970 ワード
重要なプロファイル:
db.properties
log4j.properties略
SqlMapConfig.xml
DAO構成、ApplicationContest-dao.xml
サービスの構成xml
事物配置,ApplicationContext-trans.xml、注釈の使用が便利
注記トランザクションと接面トランザクションは1つだけ残したほうがいい!!!
SpringMvc.xml
web.xml
キーコード:
Serviceの実装クラス、インタフェース略、mapperおよびエンティティクラスは、データベースに基づいて逆方向に直接生成できます.
Controller
Restful
パラメータのデフォルト値と指定パラメータ名は、次の例ではidというパラメータがパラメータparamで受信され、ない場合は0であることを示します.
リダイレクトと転送
@ResponseBody NULLフィールドは無視
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin
log4j.properties略
SqlMapConfig.xml
DAO構成、ApplicationContest-dao.xml
サービスの構成xml
事物配置,ApplicationContext-trans.xml、注釈の使用が便利
注記トランザクションと接面トランザクションは1つだけ残したほうがいい!!!
SpringMvc.xml
web.xml
ssm0523
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath:ApplicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springMvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:SpringMvc.xml
1
springMvc
*.action
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
キーコード:
Serviceの実装クラス、インタフェース略、mapperおよびエンティティクラスは、データベースに基づいて逆方向に直接生成できます.
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper;
@Override
public List list() throws Exception {
// , example new
ItemsExample example = new ItemsExample();
// item blob ,
List list = itemsMapper.selectByExampleWithBLOBs(example);
return list;
}
...
}
Controller
@Controller
public class ItemsController {
@Autowired
private ItemsService itmesService;
@RequestMapping("/list")
public ModelAndView itemsList() throws Exception{
// DAO
List list = itmesService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
// ,
modelAndView.setViewName("itemList");
return modelAndView;
}
//
//
// , Struts2 , : , : .
// A ,String s; B b;
// B String ss;
// s=xxx,b.ss=xxx。
//
//
/**
* springMvc : controller , , .
* HttpServletRequest
* HttpServletResponse
* HttpSession
* Model
*/
@RequestMapping("/itemEdit")
public String itemEdit(HttpServletRequest reuqest,
Model model) throws Exception{
String idStr = reuqest.getParameter("id");
Items items = itmesService.findItemsById(Integer.parseInt(idStr));
//Model :
//model request , request .
model.addAttribute("item", items);
// springMvc string , springMvc
return "editItem";
}
//springMvc , string.spirngMvc .
//controller input name
//public String update(Integer id, String name, Float price, String detail) throws Exception{
//spirngMvc pojo : input name pojo
@RequestMapping("/updateitem")
public String update(Items items) throws Exception{
itmesService.updateItems(items);
return "success";
}
// Controller Vo, input name vo . . .....
@RequestMapping("/search")
public String search(QueryVo vo) throws Exception{
System.out.println(vo);
return "";
}
// , QueryVo , Integer[] ids, ids 。 ids
public String search(QueryVo vo, Integer[] ids) throws Exception{
...
return "";
}
// , Items List items, QueryVo ,
}
Restful
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;
@Controller
// : conroller , url ,
@RequestMapping("xxx")
public class ItemController {
@Autowired
private ItemService itemService;
// @PathVariable , , itemId , , (@PathVariable Long itemId)
// @ResponseBody , json , tbItem json , Jackson
// @RequestMapping(value="/list", method=RequestMethod.GET)
@RequestMapping("/item/{itemId}")
@ResponseBody
public TbItem getItemById(@PathVariable("itemId") Long itemId) {
TbItem tbItem = itemService.getItemById(itemId);
return tbItem;
}
@RequestMapping("/item/list")
@ResponseBody
public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
EasyUIDataGridResult result = itemService.getItemList(page, rows);
return result;
}
}
パラメータのデフォルト値と指定パラメータ名は、次の例ではidというパラメータがパラメータparamで受信され、ない場合は0であることを示します.
public EasyUIDataGridResult getItemList(@RequestParam(name="id", defaultValue="0")Integer param) {
EasyUIDataGridResult result = itemService.getItemList(page, rows);
return result;
}
リダイレクトと転送
public String update(MultipartFile pictureFile,Items items, Model model, HttpServletRequest request) throws Exception{
// : url ,request
// 1.
model.addAttribute("id", items.getId());
// springMvc redirect:
return "redirect:itemEdit"; //
// 2. restful
return "redirect:itemEdit/"+items.getId();
// ,itemEdit , restful
// redirect forward
// ------------ :-------------
// , , , / , :
// redirect:itemEdit redirect:/itemEdit, ip:port/ ( RequestMapping)/itemEdit ip:port/itemEdit
// , , controller
}
@ResponseBody NULLフィールドは無視
NON_NULL