DemoController.java
package com.sp.demo;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller("demo.demoController")
public class DemoController {
@Autowired
private DemoService service;
@RequestMapping(value="/demo/write", method=RequestMethod.GET)
public String form(){
return "demo/write";
}
@RequestMapping(value="/demo/write", method=RequestMethod.POST)
public String submit(HttpServletRequest req, Demo demo){
String msg="추가 성공";
try{
service.insertDemo(demo);
}catch(Exception e){
System.out.println(e.toString());
msg="추가 실패.";
}
req.setAttribute("message", msg);
return "demo/write_ok";
}
}
=========================================================================================================================================================
DemoService.java
package com.sp.demo;
public interface DemoService {
public int insertDemo(Demo demo) throws Exception;
}
=========================================================================================================================================================
package com.sp.demo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly=true)
public class DemoServiceImpl implements DemoService {
private DemoDAO demoDao;
public void setDemoDao(DemoDAO demoDao){
this.demoDao = demoDao;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class}) // 롤백처리하는 어노테이션
public int insertDemo(Demo demo) throws Exception{ // 오버라이드 할때는 예외를 뺄순 있어도 추가할수는 없다. 쓰려면 interface를 수정해야한다.
int result=0;
try{
demoDao.insertDemo(demo);
result=1;
}catch(Exception e){
throw e;
}
return result;
}
}
'spring' 카테고리의 다른 글
스프링 정적 페이지 처리 static data control (0) | 2016.07.17 |
---|---|
AbstractApplicationContext 스프링 설정파일 여러개 사용하기 (0) | 2014.05.02 |
스프링 시큐리티 설정 (0) | 2013.08.10 |