1 / 22

Struts2 迁移 SpringMVC 分享

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

sahara
Download Presentation

Struts2 迁移 SpringMVC 分享

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Struts2

  4. <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>

  5. SpringMVC

  6. <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>

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

  8. 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>

  9. 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); } }

  10. <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"/>

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

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

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

  14. 3、@InitBinder问题

  15. DateEditor private 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) : ""); }

  16. 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;

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

  18. 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); }

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

  20. 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("数据有问题!"); }

  21. Thanks for Listening!

More Related