2.Struts2和SpringMVC共存

Report
Struts2迁移SpringMVC分享
王海南 DJ Job组
2012年12月21日
目录
• 1.Struts2和SpringMVC原理简介
• 2.Struts2和SpringMVC共存
• 3.Spring修改内容
Struts2
• <filter>
•
<filter-name>struts2</filter-name>
•
<filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFi
lter</filter-class>
•
</filter>
•
<filter-mapping>
•
<filter-name>struts2</filter-name>
•
<url-pattern>/*</url-pattern>
•
<dispatcher>REQUEST</dispatcher>
•
<dispatcher>INCLUDE</dispatcher>
•
</filter-mapping>
SpringMVC
• <servlet>
•
<servlet-name>job</servlet-name>
•
<servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass>
•
<load-on-startup>1</load-on-startup>
•
</servlet>
•
<servlet-mapping>
•
<servlet-name>job</servlet-name>
•
<url-pattern>/*</url-pattern>
•
</servlet-mapping>
• 1.Struts2和SpringMVC原理简介
• 2.Struts2和SpringMVC共存
• 3.Spring修改内容
• 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>
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);
}
}
• <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.CustomFreema
rkerManagerWithEscape"/>
• <constant name="struts.enable.DynamicMethodInvocation"
value="false"/>
• 1.Struts2和SpringMVC原理简介
• 2.Struts2和SpringMVC共存
• 3.Spring修改内容
1、 <mvc:annotation-driven/>
<mvc:annotation-driven/>会自动注册DefaultAnnotationHandlerMapping
与AnnotationMethodHandlerAdapter两个bean,是spring MVC为
@Controllers分发请求所必须的
并提供了:数据绑定支持,@NumberFormatannotation支持,
@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读
写JSON的支持(Jackson)
2、404问题
throw new ResourceNotFoundException("ResumePositionController edit
parameter id is error. id: " + id);
[email protected]
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) : "");
}
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 + "WEBINF/ftl/resume/", "frag_practice.ftl", dataMap, true);
} catch (Exception e) {
log.error("preview parseTemplate Error:" + modelType);
}
return content;
@ResponseBody中文乱码问题
StringHttpMessageConverter中设置的默认编码为ISO-8859-1
DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
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);
}
private static final DataBind<Resume> RESUME_DATA_BIND =
DataBindManager.getInstance().getDataBind(DataBindTypeEnum.RESUME);
RESUME_DATA_BIND.put(resume);
RESUME_DATA_BIND.get()
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("数据有问题!");
}

similar documents