http://www.javajigi.net |
SpringMVC
[출처] SpringMVC 환경설정 (비공개 카페)
|
여태까지는 Servlet / JSP배우면서 Model2 MVC를 흉내내거나
spring넘어와서도 Java Project 생성 후 Model2 MVC를 흉내내는 수준의 작업이였다.
- 환경설정 및 개발단계
1. spring library 를 maven으로 추가
2. log4j 설정파일확인 (선택적)
3. web.xml에서
springframework의 DispatcherServlet 설정 추가
( servlet-name 과 servlet-class , url-pattern )
[servlet name]-servlet.xml 로 springmvc framework 설정
파일을 기본적을 찾게 된다.
4. url-pattern 을 *.do 형태로 지정하여
.do로 마치는 모든 요청을 spring의 DispatcherServlet이 처리
하도록 설정한다.
5. springmvc 설정파일
1) HandlerMapping 등록
2) ViewResolver 등록
3) 사용될 컨트롤러를 등록
이동할 URL을 *.do로 설정하여 끝나는 부분이 do인 url을 걸러낸다
* web.xml *
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
| cs |
.do로 들어온 url을 CustomerController로 보낸다
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
HandlerMapping 등록
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
viewResolver 등록
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp"/>
</bean>
<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="command"/>
</bean>
<bean id="customerDAO" class="model.CustomerDAOImpl"> </bean>
<bean id="customerService" class="model.CustomerServiceImpl">
<constructor-arg ref="customerDAO"/>
</bean>
사용될 컨트롤러를 등록
<bean id="/customer.do" class="controller.CustomerController">
<constructor-arg ref="customerService"/>
<property name="methodNameResolver" ref="methodNameResolver"/>
</bean>
</beans>
| cs |
위처럼 xml을 설정한 후 id를 검색한다.
* index.jsp *
<form action = "customer.do">
<input type = "hidden" name = "command" value = "findCustomerById">
아이디 <input type = "text" name = "id">
<input type = "submit" value = "검색">
</form>
| cs |
* STEP1 -> find_ok.jsp or find_fail.jsp *
public class CustomerController extends MultiActionController {
private CustomerService customerService;
public CustomerController(CustomerService customerService) {
super();
this.customerService = customerService;
}
public ModelAndView findCustomerById
(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
CustomerVO cvo = customerService.findCustomerById(id);
if (cvo == null)
return new ModelAndView("find_fail");
else
return new ModelAndView("find_ok", "cvo", cvo);
}
}
* STEP2 *
public class CustomerServiceImpl implements CustomerService {
private CustomerDAO customerDAO;
public CustomerServiceImpl(CustomerDAO customerDAO) {
super();
this.customerDAO = customerDAO;
}
@Override
public CustomerVO findCustomerById(String id){
return customerDAO.findCustomerById(id);
}
}
* STEP3 -> STEP1 *
(DB없이 test)
public class CustomerDAOImpl implements CustomerDAO {
@Override
public CustomerVO findCustomerById(String id) {
CustomerVO vo = null;
if (id.equals("java"))
vo = new CustomerVO(id, "손재만", "판교");
return vo;
}
}
| cs |
아직도 XML 사용방법에 대해서는 어리둥절하고 있지만
확실히 springMVC 구조가 잡히는게 눈에 보이고있다.
0 개의 댓글:
댓글 쓰기