struts2 迁移 springmvc 分享

22
Struts2 迁迁 SpringMVC 迁迁 迁迁迁 DJ Job 迁 2012 迁 12 迁 21 迁

Upload: sahara

Post on 25-Feb-2016

288 views

Category:

Documents


10 download

DESCRIPTION

Struts2 迁移 SpringMVC 分享. 王海南 DJ Job 组 2012 年 12 月 21 日. 目录. 1.Struts2 和 SpringMVC 原理简介 2.Struts2 和 SpringMVC 共存 3.Spring 修改内容. Struts2. struts2 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Struts2 迁移 SpringMVC 分享

Struts2 迁移 SpringMVC 分享王海南 DJ Job 组2012 年 12 月 21 日

Page 2: Struts2 迁移 SpringMVC 分享

目录• 1.Struts2 和 SpringMVC 原理简介• 2.Struts2 和 SpringMVC 共存• 3.Spring 修改内容

Page 3: Struts2 迁移 SpringMVC 分享

Struts2

Page 4: Struts2 迁移 SpringMVC 分享

• <filter>• <filter-name>struts2</filter-name>• <filter-

class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

• </filter>• <filter-mapping>• <filter-name>struts2</filter-name>• <url-pattern>/*</url-pattern>• <dispatcher>REQUEST</dispatcher>• <dispatcher>INCLUDE</dispatcher>• </filter-mapping>

Page 5: Struts2 迁移 SpringMVC 分享

SpringMVC

Page 6: Struts2 迁移 SpringMVC 分享

• <servlet>• <servlet-name>job</servlet-name>• <servlet-

class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

• <load-on-startup>1</load-on-startup>• </servlet>• <servlet-mapping>• <servlet-name>job</servlet-name>• <url-pattern>/*</url-pattern>• </servlet-mapping>

Page 7: Struts2 迁移 SpringMVC 分享

• 1.Struts2 和 SpringMVC 原理简介• 2.Struts2和 SpringMVC共存• 3.Spring 修改内容

Page 8: Struts2 迁移 SpringMVC 分享

• 1、 web.xml<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping>

<servlet> <servlet-name>job</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>job</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

Page 9: Struts2 迁移 SpringMVC 分享

if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { chain.doFilter(request, response); } else { request = prepare.wrapRequest(request); ActionMapping mapping = prepare.findActionMapping(request,

response, true); if (mapping == null) { boolean handled = execute.executeStaticResourceRequest(request,

response); if (!handled) { chain.doFilter(request, response); } } else { execute.executeAction(request, response, mapping); } }

Page 10: Struts2 迁移 SpringMVC 分享

• <constant name="struts.enable.SlashesInActionNames" value="true"/>

• <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

• <constant name="struts.action.excludePattern" value="/resin-status,/resume/.*"/>

• <constant name="struts.freemarker.manager.classname" value="com.dajie.common.framework.struts.CustomFreemarkerManagerWithEscape"/>

• <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

Page 11: Struts2 迁移 SpringMVC 分享

• 1.Struts2 和 SpringMVC 原理简介• 2.Struts2 和 SpringMVC 共存• 3.Spring修改内容

Page 12: Struts2 迁移 SpringMVC 分享

1 、 <mvc:annotation-driven/>

<mvc:annotation-driven/> 会自动注册DefaultAnnotationHandlerMapping 与 AnnotationMethodHandlerAdapter两个 bean, 是 spring MVC 为 @Controllers 分发请求所必须的并提供了:数据绑定支持, @NumberFormatannotation 支持, @DateTimeFormat 支持, @Valid 支持,读写 XML 的支持( JAXB ),读写 JSON 的支持( Jackson )

Page 13: Struts2 迁移 SpringMVC 分享

2 、 404 问题throw new ResourceNotFoundException("ResumePositionController edit parameter id is error. id: " + id);

Page 14: Struts2 迁移 SpringMVC 分享

3 、 @InitBinder 问题

Page 15: Struts2 迁移 SpringMVC 分享

DateEditorprivate static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd")private static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");private DateFormat dateFormat;private boolean allowEmpty = false;

public String getAsText() { Date value = (Date) getValue(); DateFormat dateFormat = this.dateFormat; if (dateFormat == null) dateFormat = DATEFORMAT; return (value != null ? dateFormat.format(value) : ""); }

Page 16: Struts2 迁移 SpringMVC 分享

4 、 Ajax 提交问题Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("resume", resume); String webRoot = ServletUtil.getWebRoot(); if (StringUtil.isNotEmpty(webRoot) && !webRoot.endsWith("/")) { webRoot += "/"; } String content = ""; try { content = TemplateUtil.parseTemplate4EscapeHtml(webRoot + "WEB-

INF/ftl/resume/", "frag_practice.ftl", dataMap, true); } catch (Exception e) { log.error("preview parseTemplate Error:" + modelType); }return content;

Page 17: Struts2 迁移 SpringMVC 分享

@ResponseBody 中文乱码问题StringHttpMessageConverter 中设置的默认编码为 ISO-8859-1DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

Page 18: Struts2 迁移 SpringMVC 分享
Page 19: Struts2 迁移 SpringMVC 分享

5 、 DataBindManager 问题public boolean preHandle(HttpServletRequest request, HttpServletResponse

response, Object handler) throws Exception {Resume resume = resumeService.getResumeByUid(userBase.getUid()); request.setAttribute("resume",resume);return super.preHandle(request, response, handler);}

@RequestMapping(value = "index")public ModelAndView index(HttpServletRequest request) {Resume resume = getUserResume();return new ModelAndView("resume/index", "resume", resume);

}

Page 20: Struts2 迁移 SpringMVC 分享

private static final DataBind<Resume> RESUME_DATA_BIND = DataBindManager.getInstance().getDataBind(DataBindTypeEnum.RESUME);

RESUME_DATA_BIND.put(resume);

RESUME_DATA_BIND.get()

Page 21: Struts2 迁移 SpringMVC 分享

6 、 validator

@Autowired@Qualifier("resumePracticeValidator")private ResumePracticeValidator resumePracticeValidator;

final BindException errors = new BindException(practice, "practice");resumePracticeValidator.validate(practice, errors);if (errors.hasErrors()) {

return AjaxModel.failed(" 数据有问题! ");}

Page 22: Struts2 迁移 SpringMVC 分享

THANKS FOR LISTENING!