spring Mvc 와 struts 2
8067 단어 spring
1. spring mvc 의 입문, google 어떤 demo 를 하지 마 세 요. 공식 적 인 이것 이 가장 좋 을 것 입 니 다.
http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/
2. 좋 은 글 두 편 을 추천 합 니 다. spring reference 만 보면 좀 더 깊이 들 어가 기 어 려 울 것 같 습 니 다.
실전 에 들 어가 서 이거 봐.
http://elf8848.iteye.com/blog/875830
소스 코드 이거 봐.
http://huibin.iteye.com/blog/618910
본론:
struts 2 를 사용 한 지 이렇게 오래 되 었 지만 springmv 는 계속 보 았 습 니 다. 실전 을 해 본 적 이 없습니다. mvc 모델 에 따라 두 가 지 를 비교 하고 다음 과 같은 몇 가지 측면 에서 그들의 차 이 를 이야기 하고 싶 습 니 다.
1. 각도 차단
차단 처 리 를 요청 하 는 것 은 모두 chain 방식 입 니 다. 사용 하 는 것 은 어 떻 습 니까?
입구 하 나 는 fiter 이 고 하 나 는 server. lt 입 니 다. 모든 요청 은 chain 처 리 를 하고 실현 방식 이 다 르 며 효과 가 유사 합 니 다.
struts2
갈고리 인자 hook -- > Action Invocation 이 있 습 니 다.
public interface Interceptor extends Serializable {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
}
spring 은 세 가지 방법 으로 나 뉘 어 각각 호출 해 야 하지만 hook 방식 은 하나 도 없다.
// Interceptor
if (mappedHandler.getInterceptors() != null ) {
for ( int i = 0 ; i < mappedHandler.getInterceptors().length; i++) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null );
return ;
}
interceptorIndex = i;
}
}
// handler , HandlerAdapter handler : Spring 。
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// Interceptor
if (mappedHandler.getInterceptors() != null ) {
for ( int i = mappedHandler.getInterceptors().length - 1 ; i >= 0 ; i--) {
HandlerInterceptor interceptor = mappedHandler.getInterceptors()[i];
interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
}
}
}
설정 한 각도:
strus 2 는 package 구분 이 있 고 구체 적 인 유형 으로 구분 되 며 intercepter 를 설정 할 수 있 으 며 차단기 스 택 도 있 고 hook 이 존재 하 며 action 위의 정 보 를 호출 할 수 있 습 니 다. 이것 은 action 설정 의 주석 에 따라 처리 할 수 있 습 니 다.
spring 설정 은 구체 적 인 처리 컨트롤 러 나 경로 에 따라 뚜렷 한 path 를 구분 해 야 합 니 다.
이 struts 2 는 spring 보다 실용성 이 좋 지만 struts 2 의 효과 spring 도 가능 합 니 다.
2. 매개 변수 처리 각도
http 의 string 형식 을 자바 형식 으로 변환 합 니 다.
자바 형식 은 구체 적 인 업무 클래스 로 처리 되 고 있 습 니 다.
두 번 의 전환 은 모두 작은 디 테 일 이 있 습 니 다. 듣 기 좋 은 점 은 임피던스 가 일치 하지 않 고 struts 2 의 방식 이 약간 무 겁 습 니 다. 예 를 들 어 유형 전환 model Driver 방식 은 선택 할 수 있 고 특수 처 리 를 할 수 있 지만 spring 의 유연성 에 비해 차이 가 많 습 니 다.
spring 은 주 해 를 통 해 특정 인 자 를 주입 하 는 것 을 선택 할 수 있 습 니 다. 번 거 로 우 며 strus 2 방식 도 선택 할 수 있 습 니 다. 설정 하지 않 으 면 자동 으로 주입 되 고 서명 한 session 등급 의 처리 가 잘 되 어 session 을 선명 하 게 할 수 있 습 니 다.
이 점 은 스프링 이 struts 2 보다 낫다 고 생각 합 니 다. 구체 적 으로 자 료 를 참고 할 수 있 습 니 다.
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/
3. 보기 처리 각도
struts 2 valuestack 및 세트 로 된 OGNL 은 매우 강하 지만 jstl 은 주류 인 것 같 습 니 다. ognl 에 첨부 된 호출 방법 은 무엇 입 니까? view 층 은 항상 파괴 되 었 다 고 생각 합 니 다.
spring 은 model 방식 을 통 해 도 좋 습 니 다. 매우 유연 합 니 다. 보 여줄 것 을 model 에 넣 으 려 면 이름 을 짓 는 규칙 을 잘 정의 해 야 합 니 다. struts 2 와 같 지 않 습 니 다. 구체 적 인 매개 변 수 는 action 에 있 습 니 다.
4. 이상 처리 각도
spring 의 이상 은 스스로 설정 할 수 있 고 그의 < global - exception - mappings > 와 유사 한 동쪽 도 사용 할 수 있 으 며 자신 이 쓴 이상 처리 류 도 있 습 니 다.
전역 이상 과 패키지 이상 처리 로 나 뉘 지만 프레임 워 크 와 인 터 페 이 스 를 실현 할 필요 가 없습니다. spring 은 Handler Exception Resolver 를 다음 과 같이 실현 해 야 합 니 다.
// catch controller , ExceptionResolver , , ,
catch (Exception ex) {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(processedRequest, response, handler, ex);
errorView = (mv != null);
}
}
// ExceptionResolver , ,
catch (Exception ex) {
// Trigger after-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
}
catch (Error err) {
ServletException ex = new NestedServletException("Handler processing failed", err);
// Trigger after-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
throw ex;
}
//
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
// Check registered HandlerExceptionResolvers...
ModelAndView exMv = null;
for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
if (exMv != null) {
break;
}
}
struts 2 는 독특한 정 보 를 얻 을 수 없고 이상 처리 클래스 만 설정 할 수 있 습 니 다. 그러나 strusts 2 의 차단 은 처리 프로 세 스 정 보 를 얻 고 이상 처 리 를 할 수 있 습 니 다.
4. 스프링 프레임 과 의 융합
spring Mvc 와 spring 가족, spring 은 잘 어 울 리 지만
구체: spring test 시원, action 테스트 가능
struts 2 플러그 인 을 통 해 통합 되 었 습 니 다. 구체 적 으로 springmvc 보다 못 한 것 은 잠시 발견 되 지 않 았 습 니 다.
6. 잡담 - 특수 수요 확장 만족 은 어 떻 습 니까? 인기 restful 은 어 떻 습 니까?
struts 2 플러그 인 은 restful 을 할 수 있 습 니 다. 이것 은 strus 2 플러그 인 이 특별한 일 을 할 수 있다 는 것 을 설명 합 니 다. spring 도 restful 방식 을 지원 할 수 있 습 니 다. 비록 우리 가 지금 하고 있 는 것 은 인터넷 프로젝트 이지 만 restful 을 돌리 지 않 았 습 니 다. 두 가 지 는 이해 가 부족 하고 어려움 을 바 꾸 며 관망 학습 중 입 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.