锘??xml version="1.0" encoding="utf-8" standalone="yes"?>package com.i18n;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class I18nFilter implements Filter
{
public void destroy()
{}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) req;
MyRequestWrapper wrapper = new MyRequestWrapper(request);
filterChain.doFilter(wrapper, resp);
}
public void init(FilterConfig arg0) throws ServletException
{}
}
2.榪囨護鍣ㄩ厤緗?br />
<filter>
<filter-name>i18nFilter</filter-name>
<filter-class>com.xxx.common.filter.I18nFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>i18nFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
3.鍖呰綾?br />
package com.i18n;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
public class MyRequestWrapper extends HttpServletRequestWrapper
{
private Locale locale = null;
public MyRequestWrapper(HttpServletRequest request)
{
super(request);
HttpSession session = request.getSession();
locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
}
public String getHeader(String name)
{
String value = super.getHeader(name);
if ("Accept-Language".equals(name) && locale != null)
{
value = locale.getLanguage() + "_" + locale.getCountry()
+ value.substring(6, value.length());
}
return value;
}
public Locale getLocale()
{
if (locale != null)
{
return locale;
}
return super.getLocale();
}
}
]]>
鐐瑰嚮sowftware update 鑿滃崟,
鍐嶇偣find and install
閫夋嫨seach for new feathures to install ,鐐瑰嚮next
鐐瑰嚮鍙寵竟絎竴涓篃灝辨槸 new remote site閭d釜鎸夐敭, 闅忎究涔辮緭鍏ヤ竴涓悕瀛椼傜綉鍧灝辮緭鍏ヤ笅闈㈣繖涓?
榪欐槸緗戝潃 http://propedit.sourceforge.jp/eclipse/updates/
鎴? window-->preferences-->content types-->java properties file
鏀逛負GBK鎴朥TF-8 , 鐒跺悗update, 紜畾.
濡傛灉鍐嶆湁闂, 鍒欐壘鍒?.properties鐨勮祫婧愭枃浠訛紝鎶婄紪杈戝櫒鏀逛負PropertiesEditor.
]]>
]]>package org.rw.commons.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
*
* <p>
* Title:[鑷畾涔夋棩鏈熻漿鍖栫被]
* </p>
* <p>
* Description: [鎶婂瓧絎︿覆杞寲鎴愭棩鏈熸垨鎶婃棩鏈熻漿鍖栨垚瀛楃涓瞉
* </p>
* <p>
* Copyright 2009 Huyvanpull Co., Ltd.
* </p>
* <p>
* All right reserved.
* </p>
*
* @version 1.0
*
* <p>
* Created by [Huyvanpull] [Mar 26, 2009]
* </p>
* <p>
* Midified by [淇敼浜篯 [淇敼鏃墮棿]
* </p>
*
*/
public class DateConverter extends StrutsTypeConverter
{
/** 榛樿鐨勬棩鏈熻漿鎹㈡牸寮?nbsp;*/
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm";
/** 鍙帴鍙楃殑鏃ユ湡鏍煎紡 */
public static final DateFormat[] ACCEPT_DATE_FORMATS = {
new SimpleDateFormat(DEFAULT_DATE_FORMAT),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") };
@SuppressWarnings("unchecked")
public Object convertFromString(Map context, String[] values, Class toClass)
{
if (values[0] == null || values[0].trim().equals(""))
{
return null;
}
/** 灝濊瘯浠庨粯璁ゅ煎紑濮嬭漿鎹?nbsp;*/
for (DateFormat format : ACCEPT_DATE_FORMATS)
{
try
{
return format.parse(values[0]);
}
catch (Exception e)
{}
}
return null;
}
@SuppressWarnings("unchecked")
public String convertToString(Map context, Object o)
{
if (o instanceof Date)
{
try
{
return ACCEPT_DATE_FORMATS[0].format((Date) o);
}
catch (RuntimeException e)
{
return "";
}
}
return "";
}
}
灞閮ㄩ厤緗柟娉?
QueryDataAction-conversion.properties
startTime = org.rw.commons.util.DateConverter
endTime = org.rw.commons.util.DateConverter
]]>
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author Huyvanpull
*
*/
@SuppressWarnings("serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
private String interceptorName;
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
this.logger.debug("榪涘叆" + this.getInterceptorName());
String result = null;
try
{
result = invocation.invoke();
}
catch (Exception exception)
{
this.logger.error(this.getExceptionInfo(exception));
throw exception;
}
return result;
}
private String getExceptionInfo(Exception exception)
{
StringBuffer bExceptionInfo = new StringBuffer();
bExceptionInfo.append(exception.toString());
bExceptionInfo.append("\n\t");
StackTraceElement[] stackTraceElements = exception.getStackTrace();
for (int i = 0; i < stackTraceElements.length; i++)
{
bExceptionInfo.append("[" + this.getInterceptorName() + "] "
+ stackTraceElements[i].toString() + "\n\t");
}
return bExceptionInfo.toString();
}
public String getInterceptorName()
{
return interceptorName;
}
public void setInterceptorName(String interceptorName)
{
this.interceptorName = interceptorName;
}
}
鐧婚檰媯鏌ユ嫤鎴櫒:
import java.util.Map;
import org.apache.log4j.Logger;
import org.roadway.lm.po.UserInfo;
import org.roadway.lm.user.action.UserInfoAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author tom_hui
*
*/
@SuppressWarnings("serial")
public class LoginCheckerInterceptor extends AbstractInterceptor
{
private String userSessionKey = "userInfo";
private String isCheckLogin = "true";
private Logger logger = Logger
.getLogger(LoginCheckerInterceptor.class);
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation actionInvocation) throws Exception
{
Object action = actionInvocation.getAction();
/** 濡傛灉璁劇疆鎷︽埅鍣ㄤ笉媯鏌ョ櫥闄?nbsp;*/
if ("false".equalsIgnoreCase(isCheckLogin))
{
actionInvocation.invoke();
}
/** 濡傛灉鏄櫥闄咥ction,鏀懼叾閫氳 */
if (action instanceof UserInfoAction)
{
this.logger.info("鐧婚檰Action:" + UserInfoAction.class.getName());
return actionInvocation.invoke();
}
/** 浠巗ession涓緱鍒癠serInfo鐨勪俊鎭?nbsp;*/
Map session = actionInvocation.getInvocationContext().getSession();
UserInfo userInfo = (UserInfo) session.get(userSessionKey);
/** 濡傛灉Session涓瓨鍦║serInfo瀵硅薄 */
if (userInfo != null)
{
this.logger.info("鐢ㄦ埛" + userInfo.getUserName() + "("
+ userInfo.getUserId() + ")鐧婚檰浜?");
return actionInvocation.invoke();
}
/** 濡傛灉娌℃湁鐧婚檰 */
else
{
return Action.LOGIN;
}
}
public String getUserSessionKey()
{
return userSessionKey;
}
public void setUserSessionKey(String userSessionKey)
{
this.userSessionKey = userSessionKey;
}
public String getIsCheckLogin()
{
return isCheckLogin;
}
public void setIsCheckLogin(String isCheckLogin)
{
this.isCheckLogin = isCheckLogin;
}
}
]]>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.encoding" value="gb2312" />
<constant name="struts.custom.i18n.resources"
value="messageResource" />
<constant name="struts.action.extension" value="jspa" />
<constant name="struts.multipart.saveDir" value="/tmp" />
<constant name="struts.multipart.maxSize" value="10000000" />
<constant name="struts.ui.theme" value="simple" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="exceptionInterceptor"
class="org.roadway.wisp.zd.util.ExceptionInterceptor">
<param name="interceptorName">
self-interceptor
</param>
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="exceptionInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
<global-exception-mappings>
<exception-mapping result="error"
exception="java.lang.Exception" />
</global-exception-mappings>
</package>
<include file="register.xml" />
</struts>
]]>
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author Huyvanpull
*
*/
@SuppressWarnings("serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
private String interceptorName;
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
this.logger.debug("榪涘叆" + this.getInterceptorName());
String result = null;
try
{
result = invocation.invoke();
}
catch (Exception exception)
{
this.logger.error(this.getExceptionInfo(exception));
throw exception;
}
return result;
}
private String getExceptionInfo(Exception exception)
{
StringBuffer bExceptionInfo = new StringBuffer();
bExceptionInfo.append(exception.toString());
bExceptionInfo.append("\n\t");
StackTraceElement[] stackTraceElements = exception.getStackTrace();
for (int i = 0; i < stackTraceElements.length; i++)
{
bExceptionInfo.append("[" + this.getInterceptorName() + "] "
+ stackTraceElements[i].toString() + "\n\t");
}
return bExceptionInfo.toString();
}
public String getInterceptorName()
{
return interceptorName;
}
public void setInterceptorName(String interceptorName)
{
this.interceptorName = interceptorName;
}
}
]]>
FreeMarker鐨勯氱敤鐩爣錛氳兘澶熺敓鎴愬悇縐嶆枃鏈紝HTML銆乆ML銆丷TF銆丣ava婧愪唬鐮佺瓑絳夛紱鏄撲簬宓屽叆錛岃交閲忕駭銆佷笉闇瑕丼ervlet鐜錛涙彃浠跺紡鐨勬ā鐗堣澆鍏ュ櫒錛屽彲浠ュ湪浠諱綍婧愯澆鍏ワ紝濡傛湰鍦版枃浠躲佹暟鎹簱絳夌瓑錛涙寜闇鐢熸垚鏂囨湰錛屼繚瀛樺埌鏈湴鏂囦歡銆佷綔涓篍mail鍙戦併佷粠Web搴旂敤紼嬪簭鍙戦佸畠榪斿洖鍒癢eb嫻忚鍣ㄣ?
寮哄ぇ鐨勬ā鐗堣璦錛氭墍鏈夊父鐢ㄧ殑鎸囦護錛宨nclude銆乮f/elseif/else銆佸驚鐜粨鏋勩傚湪妯℃澘涓垱寤哄拰鏀瑰彉鍙橀噺銆傚彲浠ヤ嬌鐢ㄥ鏉傜殑琛ㄨ揪寮忔潵浣跨敤鍊箋傚懡鍚嶇殑瀹忥紝鍙互鍏鋒湁浣嶇疆鍙傛暟鍜屽祵濂楀唴瀹廣傚悕瀛楃┖闂存湁鍔╀簬寤虹珛鍜岀淮鎶ゅ彲閲嶇敤鐨勫畯搴擄紝鎴栬呭皢涓涓ぇ鐨勫伐紼嬪垎涓鴻嫢騫蹭釜妯″潡錛岃屼笉蹇呮媴蹇冨懡鍚嶇殑鍐茬獊銆傝緭鍑鴻漿鎹㈠潡錛屽湪宓屽妯$増鐗囨柇鐢熸垚杈撳嚭鏃訛紝杞崲HTML杞箟銆佸帇緙┿佽娉曢珮浜瓑絳夈傘?
閫氱敤鐨勬暟鎹ā鍨嬶細FreeMarker涓嶆槸鐩存帴鍙嶆槧鍒癹ava瀵硅薄錛宩ava瀵硅薄閫氳繃鎻掍歡寮忓皝瑁咃紝浠ュ彉閲忕殑鏂瑰紡鍦ㄦā鐗堜腑灞曠ず銆備嬌鐢ㄦ娊璞$殑鏂瑰紡琛ㄧず瀵硅薄錛屼嬌鍏朵笉鍙楁妧鏈粏鑺傜殑鎵撴壈銆?
飦?鍦ㄦā鏉胯璦涓唴寤哄鐞嗗吀鍨媁eb 鐩稿叧浠誨姟錛堝HTML 杞箟錛夌殑緇撴瀯
飦?鑳藉闆嗘垚鍒癕odel2Web 搴旂敤妗嗘灦涓綔涓篔SP 鐨勬浛浠?
飦?鏀寔JSP 鏍囪搴?
飦?涓篗VC 妯″紡璁捐錛氬垎紱誨彲瑙嗗寲璁捐鍜屽簲鐢ㄧ▼搴忛昏緫錛涘垎紱婚〉闈㈣璁″憳鍜岀▼搴忓憳
5銆佹櫤鑳界殑鍥介檯鍖栧拰鏈湴鍖?
飦?瀛楃闆嗘櫤鑳藉寲錛堝唴閮ㄤ嬌鐢║NICODE錛?
飦?鏁板瓧鏍煎紡鏈湴鍖栨晱鎰?
飦?鏃ユ湡鍜屾椂闂存牸寮忔湰鍦板寲鏁忔劅
飦?闈濽S 瀛楃闆嗗彲浠ョ敤浣滄爣璇嗭紙濡傚彉閲忓悕錛?
飦?澶氱涓嶅悓璇█鐨勭浉鍚屾ā鏉?
6銆佸己澶х殑XML 澶勭悊鑳藉姏
飦?<#recurse> 鍜?lt;#visit>鎸囦護錛?.3 鐗堟湰錛夌敤浜庨掑綊閬嶅巻XML 鏍?
飦?鍦ㄦā鏉夸腑娓呮鍜岀洿瑙夌殑璁塊棶XML 瀵硅薄妯″瀷
FreeMarker鏄竴涓狫ava妯$増璇█錛屽畠鏄?JSP 鐨勭粷浣蟲浛浠f柟妗? FreeMarker鍦ㄤ綘鐨刟ction result鍙兘闇瑕佸湪Servlet瀹瑰櫒鐜浠ュ琚澆鍏ョ殑鎯呭喌涓嬫槸鐞嗘兂閫夋嫨. 渚嬪,濡傛灉浣犲笇鏈涘湪浣犵殑搴旂敤紼嬪簭涓敮鎸乸lugins,浣犲彲鑳戒細涔愭剰浣跨敤Freemarker錛屽洜涓洪偅鏍風殑璇漰lugins鍙互鏀寔灝嗘墍鏈夌殑action class鍜寁iew閮芥墦鍖呭埌涓涓粠classloader榪涜瑁呰澆鐨刯ar鏂囦歡閲岄潰.
鍏充簬FreeMarker鐨勬洿澶氫俊鎭紝璇瘋闂瓼reeMarker緗戠珯.
FreeMarker涓嶸elocity闈炲父鐩鎬技, 瀹冧滑閮芥槸鍙互鍦⊿ervlet瀹瑰櫒澶栦嬌鐢ㄧ殑妯$増璇█. WebWork灝忕粍鏇存帹鑽怓reeMarker, 鑰屼笉鏄疺elocity, 榪欐槸鍥犱負FreeMarker鎻愪緵浜嗘洿濂界殑閿欒鎶ュ憡, 鏀寔JSP鏍囩, 紼嶅鐨勫姛鑳? 褰撶劧, 榪欎袱縐嶆妧鏈兘鏄唬鏇縅SP鐨勫緢濂芥柟妗?
蹇熶笂鎵?
紜閰嶇疆濂戒綘鐨勯」鐩殑classpath涓殑鎵鏈変緷璧栦互鍚? 寮濮嬩嬌鐢‵reeMarker灝遍潪甯哥畝鍗曚簡. 鍏稿瀷鎯呭喌涓嬪彧闇瑕?freemarker.jar. 闄ゅ畠浠ュ, webwork-default.xml宸茬粡閰嶇疆濂戒簡灝咶reeMarker Result鏄犲皠鍒頒綘鐨勬ā鐗堟枃浠? 浣犵幇鍦ㄥ彲浠ヨ瘯楠屼竴涓嬪涓?xwork.xml 閰嶇疆:
<action name="test" class="com.acme.TestAction">
<result name="success" type="freemarker">test-success.ftl</result>
</action
鐒跺悗鍐欏ソ test-success.ftl:
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello, ${name}
</body>
</html>
榪欓噷 name 鏄綘鐨刟ction涓殑涓涓睘鎬? 榪欐牱灝卞彲浠ヤ簡! 璇ユ枃妗g殑浣欎笅閮ㄥ垎灝嗕粙緇嶆ā鐗堝浣曡鍔犺澆, 鍙橀噺濡備綍瑙f瀽, tags(鏍囩)涔熷彲浠ヤ嬌鐢?
妯$増鍔犺澆
Webwork鍦ㄤ袱涓綅緗煡鎵綟reeMarker妯$増(鎸夐『搴?:
Web搴旂敤紼嬪簭鐩綍(Web application)
Class path
榪欎釜欏哄簭瀵逛簬鍦ㄥ畬鍏ㄧ紪璇戠殑jar涓彁渚涙ā鐗堝緢鐞嗘兂, 浣嗘槸涔熷悓鏃舵敮鎸佸湪Web搴旂敤紼嬪簭鐩綍涓畾涔夎繖浜涙ā鐗堟潵瑕嗙洊jar涓殑妯$増鏂囦歡. 浜嬪疄涓? 榪欏氨鏄負浠涔堜綘鍙互瑕嗙洊WebWork涓粯璁ょ殑UI tags鍜孎orm Tags鐨勫師鐞?
榪樻湁, 浣犲彲浠ラ氳繃templatePath 涓婁笅鏂囧彉閲?context variable)鎸囧畾涓涓礬寰?浣犵殑鏂囦歡緋葷粺涓殑涓涓洰褰?. 濡傛灉鎸囧畾浜嗚鍙橀噺, 閭d箞榪欎釜鐩綍涓殑鍐呭灝嗕細琚紭鍏堝鎵?
鍙橀噺瑙f瀽/鍐沖畾(Resolution)
鍦‵reeMarker涓? 鍙橀噺灝嗕細鍦ㄥ涓綅緗繘琛屽鎵? 欏哄簭濡備笅:
鍊兼爤(value stack)
action涓婁笅鏂?action context)
Request鑼冨洿(scope)
Session鑼冨洿(scope)
Application鑼冨洿(scope)
鍐呭緩鍙橀噺
娉ㄦ剰action涓婁笅鏂囧湪value stack鍚庤繘琛屾悳绱? 榪欐剰鍛崇潃浣犲彲浠ュ紩鐢ㄥ彉閲忚屼笉蹇呬嬌鐢ㄦ爣鍑嗙殑絎﹀彿(#)鍓嶇紑錛屼笉鍍忓湪JSP涓嬌鐢╳w:property涓繀欏諱嬌鐢ㄧ殑閭g璇硶(璇戣呮敞:鐜板湪鍦↗SP涓篃鍙互涓嶇敤#鑰岃闂粯璁ょ殑ValueStack). 榪欐槸涓涓緢濂界殑渚垮埄鐗規? 浣嗘槸灝忓績, 瀹冩湁鏃朵篃浼氭妸浣犻櫡榪涘幓. <@ww.url id="url" value="http://www.yahoo.com"/>
Click <a xhref="${url}">here</a>!
Webwork-FreeMarker鏁村悎鎻愪緵鐨勫唴寤哄彉閲忓涓?
Name Description
stack 鍊兼爤鏈韓, 鏂逛究浣跨敤 ${stack.findString('ognl expr')}鐨勬柟寮忚皟鐢?
action 鏈榪戞墽琛岀殑action
response HttpServletResponse
res 涓巖esponse鐩稿悓
request HttpServletRequest
req 涓巖eqeust鐩稿悓
session HttpSession
application ServletContext
base request鐨勪笂涓嬫枃璺緞(context path)
鏍囩鏀寔
FreeMarker鏄緢媯掔殑妯$増璇█, 鍥犱負瀹冨畬鏁寸殑鏀寔鏍囩(tag). 鍙傜収WebWork鎻愪緵鐨?FreeMarker Tags 鏂囨。涓殑濡備綍浣跨敤閫氱敤(generic) Tags 閮ㄥ垎鑾峰彇鏇村淇℃伅. 闄や簡閭d簺, 浣犺繕鍙互浣跨敤浠諱綍鐨凧SP鏍囩(tag), 灝卞儚榪欐牱:
<#assign mytag=JspTaglibs["/WEB-INF/mytag.tld"]>
<@mytag.tagx attribute1="some ${value}"/>
榪欓噷 mytag.tld 鏄綘浣跨敤鐨凧SP鏍囩搴撶殑瀹氫箟鏂囦歡. 娉ㄦ剰: 涓轟簡浣跨敤FreeMarker鐨勮繖涓敮鎸? 浣犲繀欏誨紑鍚?web.xml 2.1.x compatibility 鏂囨。涓殑 JSPSupportServlet.
鎻愮ず鍜屾妧宸?
涓嬮潰鏄湪浣跨敤FreeMarker鏋勫緩WebWork搴旂敤紼嬪簭鏃剁殑涓浜涙湁鐢ㄧ殑榪涢樁鍔熻兘.
綾誨瀷杞崲涓庢湰鍦板寲
FreeMarker鍐呯疆鏀寔鏃ユ湡涓庢暟瀛楃殑鏍煎紡鍖? 鏍煎紡鍖栫殑瑙勫垯鍩轟簬action request鐨勫湴鍖轟俊鎭?locale), locale鏄氳繃webwork.properties閰嶇疆鐨? 瀹冧篃鍙互閫氳繃I18n Interceptor榪涜瑕嗙洊. 榪欑鏂瑰紡涓鑸細瀹岀編鐨勬弧瓚充綘鐨勯渶姹? 浣嗘槸浣犺璁頒綇, 榪欎簺鏍煎紡鍖栦俊鎭槸閫氳繃FreeMarker澶勭悊鐨?鑰屼笉鏄氳繃WebWork鐨勭被鍨嬭漿鎹㈡敮鎸佸疄鐜?
濡傛灉浣犲笇鏈沇ebWork鏍規嵁浣犳墍鎸囧畾鐨勭被鍨嬭漿鎹㈠鐞嗘牸寮忓寲, 浣犱笉搴旇浣跨敤騫沖父鐨?amp;{...}璇硶. 鍙栬屼唬涔? 浣犲簲璇ヤ嬌鐢╬roperty鏍囩. 鍖哄埆鍦ㄤ簬property鏍囩鐗瑰埆涓篛GNL琛ㄨ揪寮忚璁★紝 璁$畻瀹冪殑鍊鹼紝 鐒跺悗灝嗙粨鏋滅敤浣犳寚瀹氱殑Type Conversion杞崲涓篠tring. 騫沖父浣跨敤鐨?{...}璇硶鍒欎細浣跨敤FreeMarker鐨勮〃杈懼紡璇█(EL), 璁$畻瀹冪殑鍊? 鐒跺悗閫氳繃鍐呭緩鐨勬牸寮忓寲瑙勫垯杞寲涓篠tring. 榪欎簺鍖哄埆鐢氬井, 浣嗘槸涓瀹氳浜嗚В.
鎵╁睍
鏈夋椂浣犲彲鑳介渶瑕佹墿灞昗ebWork鎻愪緵鐨凢reeMarker鏀寔. 鏈甯歌鐨勫師鍥犳槸浣犲笇鏈涘紩鍏ヤ綘鑷繁鐨勬爣絳? 灝卞儚浣犳墿灞昗ebWork鍐呭緩鏍囩涓鏍?
濡傛灉闇鎵╁睍, 棣栧厛瑕佹柊寤轟竴涓戶鎵?com.opensymphony.webwork.views.freemarker.FreemarkerManager 騫朵笖閲嶈澆浜嗙浉搴旀柟娉曠殑綾? 鐒跺悗灝嗕笅闈唬鐮佹坊鍔犲埌webwork.properties:
webwork.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager
ObjectWrapper璁劇疆
濡傛灉浣犵啛鎮変簡FreeMarker, 浣犱細鍙戠幇瀹冪殑鏁忔劅鎬т細甯︽潵涓浜涘洶鎵? 鏈甯歌鐨勬柟娉曞氨鏄皾璇曚嬌鐢‵reeMarker鎻愪緵鐨凚eanWrapper. 濡傛灉浣犱笉鐭ラ亾閭f槸浠涔?鍒媴蹇? 鍙鐭ラ亾榪欎簺閰掑彲浠ヤ簡:
WebWorkBeanWrapper緇ф壙鑷粯璁ょ殑FreeMarker BeansWrapper, 鎻愪緵浜嗗熀鏈畬鍏ㄤ竴鑷寸殑鍔熻兘, 鍙槸淇敼浜唌aps澶勭悊鏈哄埗. 涓鑸? FreeMarker鏈変袱縐嶆搷浣滄ā寮? 涓縐嶆敮鎸佸弸濂界殑鍐呯疆鐨刴ap (?key, ?values, etc),浣嗘槸鍙敮鎸丼tring浣滀負key; 鎴栬呯壒孌婄殑鍐呯疆鏀寔(渚嬪: ?key 榪斿洖map鐨勭浉搴旀柟娉曡屼笉鏄痥ey), 浣嗘槸瀹冩敮鎸丼tring鍜孲tring鐩鎬技鐨勯潪String浣滀負key. WebWork鎻愪緵浜嗕袱縐嶆儏鍐典笅鐨勫彲閫夌殑瀹炵幇鏂規.
榪欑鐗規畩鐨勫仛娉曚篃璁鎬細璁╀綘榪鋒儜鎴栦駭鐢熼棶棰? 鎵浠? 浣犲彲浠ュ皢 webwork.properties 涓殑*webwork.freemarker.wrapper.altMap*璁劇疆涓篺alse, 鍏佽鏇挎崲涓哄父瑙勭殑BeansWrapper閫昏緫.
璇硶娉ㄩ噴
濡傛灉鏄疐reeMarker 2.3.4, 榪樻敮鎸佸彟澶栫殑璇硶. 榪欑鍙夌殑璇硶鍦ㄤ綘鎰熻浣犱嬌鐢ㄧ殑IDE(灝ゅ叾鏄疘telliJ IDEA)鍦ㄩ粯璁ょ殑璇硶涓嬭繍琛屽洶闅炬椂闈炲父鏈夌敤. 鍏充簬榪欑璇硶鐨勬洿澶氬唴瀹? 璇烽槄璇昏繖閲?
struts.action.extension
The URL extension to use to determine if the request is meant for a Struts action
鐢║RL鎵╁睍鍚嶆潵紜畾鏄惁榪欎釜璇鋒眰鏄鐢ㄤ綔Struts action錛屽叾瀹炰篃灝辨槸璁劇疆 action鐨勫悗緙錛屼緥濡俵ogin.do鐨?do'瀛椼?br />
struts.configuration
The org.apache.struts2.config.Configuration implementation class
org.apache.struts2.config.Configuration鎺ュ彛鍚?br />
struts.configuration.files
A list of configuration files automatically loaded by Struts
struts鑷姩鍔犺澆鐨勪竴涓厤緗枃浠跺垪琛?br />
struts.configuration.xml.reload
Whether to reload the XML configuration or not
鏄惁鍔犺澆xml閰嶇疆(true,false)
struts.continuations.package
The package containing actions that use Rife continuations
鍚湁actions鐨勫畬鏁磋繛緇殑package鍚嶇О
struts.custom.i18n.resources
Location of additional localization properties files to load
鍔犺澆闄勫姞鐨勫浗闄呭寲灞炴ф枃浠訛紙涓嶅寘鍚?properties鍚庣紑錛?br />
struts.custom.properties
Location of additional configuration properties files to load
鍔犺澆闄勫姞鐨勯厤緗枃浠剁殑浣嶇疆
struts.devMode
Whether Struts is in development mode or not
鏄惁涓簊truts寮鍙戞ā寮?br />
struts.dispatcher.parametersWorkaround
Whether to use a Servlet request parameter workaround necessary for some versions of WebLogic
錛堟煇浜涚増鏈殑weblogic涓撶敤錛夋槸鍚︿嬌鐢ㄤ竴涓猻ervlet璇鋒眰鍙傛暟宸ヤ綔鍖猴紙PARAMETERSWORKAROUND錛?br />
struts.enable.DynamicMethodInvocation
Allows one to disable dynamic method invocation from the URL
鍏佽鍔ㄦ佹柟娉曡皟鐢?br />
struts.freemarker.manager.classname
The org.apache.struts2.views.freemarker.FreemarkerManager implementation class
org.apache.struts2.views.freemarker.FreemarkerManager鎺ュ彛鍚?br />
struts.i18n.encoding
The encoding to use for localization messages
鍥介檯鍖栦俊鎭唴鐮?br />
struts.i18n.reload
Whether the localization messages should automatically be reloaded
鏄惁鍥介檯鍖栦俊鎭嚜鍔ㄥ姞杞?
struts.locale
The default locale for the Struts application
榛樿鐨勫浗闄呭寲鍦板尯淇℃伅
struts.mapper.class
The org.apache.struts2.dispatcher.mapper.ActionMapper implementation class
org.apache.struts2.dispatcher.mapper.ActionMapper鎺ュ彛
struts.multipart.maxSize
The maximize size of a multipart request (file upload)
multipart璇鋒眰淇℃伅鐨勬渶澶у昂瀵革紙鏂囦歡涓婁紶鐢級
struts.multipart.parser
The org.apache.struts2.dispatcher.multipart.
MultiPartRequest parser implementation for a multipart request (file upload)
涓撲負multipart璇鋒眰淇℃伅浣跨敤鐨刼rg.apache.struts2.dispatcher.multipart.MultiPartRequest瑙f瀽鍣ㄦ帴鍙o紙鏂囦歡涓婁紶鐢級
struts.multipart.saveDir
The directory to use for storing uploaded files
璁劇疆瀛樺偍涓婁紶鏂囦歡鐨勭洰褰曞す
struts.objectFactory
The com.opensymphony.xwork2.ObjectFactory implementation class
com.opensymphony.xwork2.ObjectFactory鎺ュ彛錛坰pring錛?br />
struts.objectFactory.spring.autoWire
Whether Spring should autoWire or not
鏄惁鑷姩緇戝畾Spring
struts.objectFactory.spring.useClassCache
Whether Spring should use its class cache or not
鏄惁spring搴旇浣跨敤鑷韓鐨刢ache
struts.objectTypeDeterminer
The com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation class
com.opensymphony.xwork2.util.ObjectTypeDeterminer鎺ュ彛
struts.serve.static.browserCache
If static content served by the Struts filter should set browser caching header properties or not
鏄惁struts榪囨護鍣ㄤ腑鎻愪緵鐨勯潤鎬佸唴瀹瑰簲璇ヨ嫻忚鍣ㄧ紦瀛樺湪澶撮儴灞炴т腑
struts.serve.static
Whether the Struts filter should serve static content or not
鏄惁struts榪囨護鍣ㄥ簲璇ユ彁渚涢潤鎬佸唴瀹?br />
struts.tag.altSyntax
Whether to use the alterative syntax for the tags or not
鏄惁鍙互鐢ㄦ浛浠g殑璇硶鏇夸唬tags
struts.ui.templateDir
The directory containing UI templates
UI templates鐨勭洰褰曞す
struts.ui.theme
The default UI template theme
榛樿鐨刄I template涓婚
struts.url.http.port
The HTTP port used by Struts URLs
璁劇疆http绔彛
struts.url.https.port
The HTTPS port used by Struts URLs
璁劇疆https绔彛
struts.url.includeParams
The default includeParams method to generate Struts URLs
鍦╱rl涓駭鐢?榛樿鐨刬ncludeParams
struts.velocity.configfile
The Velocity configuration file path
velocity閰嶇疆鏂囦歡璺緞
struts.velocity.contexts
List of Velocity context names
velocity鐨刢ontext鍒楄〃
struts.velocity.manager.classname
org.apache.struts2.views.velocity.VelocityManager implementation class
org.apache.struts2.views.velocity.VelocityManager鎺ュ彛鍚?br />
struts.velocity.toolboxlocation
The location of the Velocity toolbox
velocity宸ュ叿鐩掔殑浣嶇疆
struts.xslt.nocache
Whether or not XSLT templates should not be cached
鏄惁XSLT妯$増搴旇琚紦瀛?/p>
1錛氬湪action涓畾涔夌殑鍙橀噺錛屽湪jsp欏甸潰涓樉紺虹敤錛?lt;s:property value="鍙橀噺鍚? />
2錛氬湪欏甸潰涓疄鐜拌嚜鍔ㄥ鍔犵殑搴忓彿鐢╥terator鐨剆tatuts鐨刬ndex灞炴?eg錛?
<s:iterator value="#request.inOutAccountList" id="data" status="listStat">
<s:property value="#listStat.index+1"/>
</s:iterator>
3:鍦╝ction綾諱腑鍙栧緱request鍜宻ession瀵硅薄鐨勬柟娉?
Map session = ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest ();
璁劇疆瀹冧滑鐨勫肩殑鏂規硶
session.put("operation", "add");
request.setAttribute("name", name);
欏甸潰涓彇寰楀畠浠殑鍊鹼細
<s:property value="#session.operation"/>
<s:property value="#request.name"/>
4:欏甸潰涓鍋惰鏍峰紡涓嶄竴鏍風殑鎺у埗鏂規硶錛?
<tr class="<s:if test='#listStat.odd == true '>tableStyle-tr1</s:if><s:else>tableStyle-tr2</s:else>" >
5:鍗曢夋鍜屽閫夋鐨勪嬌鐢ㄦ柟娉?
1):鍙互璁劇疆榛樿閫変腑鍊鹼紝娉ㄦ剰list鐨勫肩殑璁劇疆錛岄氳繃榪欑鏂瑰紡浣縦ey鍜寁alue涓嶄竴鏍鳳紝榪欑鏂規硶姣旇緝甯哥敤(checkboxlist or radio)
<s:radio name="uncarInsPolicy.policyStateCode"
list="#{'5':'閫氳繃' , '2':'涓嶉氳繃'}"
listKey="key"
listValue="value"
value='5'
/>
2):榪欓噷鐨刱ey鍜寁alue鐨勫兼槸涓鏍風殑(checkboxlist or radio)
<s:checkboxlist
list="{'Red', 'Blue', 'Green'}"
name="favoriteColor"/>
6:struts2 涓殑鏍囩浼氱敓鎴愮被浼肩敱<tr><td></td></tr>鏋勬垚鐨勫瓧涓詫紙鍏蜂綋浠涔堟爣絳劇敓鎴愪粈涔堬紝鍙互鏌ョ湅鐢熸垚鍚庣殑欏甸潰鐨勬簮浠g爜錛夊鏋滀笉闄愬埗榪欎簺澶氫綑浠g爜鐨勭敓鎴愶紝欏甸潰灝嗗彉寰楁棤娉曟帶鍒訛紝鎵浠ヤ竴鑸垜浠槸涓嶅笇鏈涘畠鐢熸垚澶氫綑鐨勪唬鐮佺殑錛屽叿浣撶殑璁劇疆鏂規硶濡傛灉錛屽湪struts.xml涓粺涓閰嶇疆
<constant name="struts.ui.theme" value="simple"/>鍔犱笂璇ュ彞鍗沖彲
涔熷彲浠ラ氳繃鍦ㄩ〉闈腑灝唗ag鐨則heme灞炴ц涓?simple"鍙栨秷鍏墮粯璁ょ殑琛ㄦ牸甯冨眬
涓嶈繃鏈濂芥槸錛氳嚜瀹氫箟涓涓猼heme錛屽茍灝嗗叾璁句負榛樿搴旂敤鍒版暣涓珯鐐癸紝濡傛涓鏉ュ氨鍙互寰楀埌緇熶竴鐨勭珯鐐歸鏍?
7錛歫sp欏甸潰涓牸寮忓寲鏃ユ湡鐨勬柟娉?
<s:date name="unCarInsModificationInfo.createTime" format="yyyy-MM-dd" nice="false"/>榪欐牱灝卞彲浠ュ皢鏃ユ湡鏍煎紡鍖栦負yyyy錛峂M錛峝d鐨勫艦寮?
8錛氶粯璁ゆ儏鍐典笅錛屽綋璇鋒眰action鍙戠敓鏃訛紝Struts榪愯鏃訛紙Runtime錛夋牴鎹畇truts.xml閲岀殑Action鏄犲皠闆?Mapping)錛屽疄渚嬪寲action瀵瑰簲鐨勭被錛屽茍璋冪敤鍏秂xecute鏂規硶銆傚綋鐒訛紝鎴戜滑鍙互閫氳繃浠ヤ笅涓ょ鏂規硶鏀瑰彉榪欑榛樿璋冪敤
1錛夊湪classes/sturts.xml涓柊寤篈ction錛屽茍鎸囨槑鍏惰皟鐢ㄧ殑鏂規硶
姣斿鎯寵皟鐢╝ction綾諱腑鐨?
public String aliasAction() {
message ="鑷畾涔堿ction璋冪敤鏂規硶";
return SUCCESS;
}
鍒欏湪classes/sturts.xml涓姞鍏ヤ笅闈唬鐮侊細
<action name="AliasHelloWorld" class="tutorial.HelloWorld" method="aliasAction">
<result>/HelloWorld.jsp</result>
</action>
鏃㈠彲鐢╝ction鍚嶈皟鐢ㄨ鏂規硶浜?
2錛夛紙姣旇緝甯哥敤錛?
璁塊棶Action鏃訛紝鍦ˋction鍚嶅悗鍔犱笂“!xxx”錛坸xx涓烘柟娉曞悕錛夈?
9錛歋truts 2.0鏈変袱涓厤緗枃浠訛紝struts.xml鍜宻truts.properties閮芥槸鏀懼湪WEB-INF/classes/涓嬨?
struts.xml鐢ㄤ簬搴旂敤紼嬪簭鐩稿叧鐨勯厤緗?
struts.properties鐢ㄤ簬Struts 2.0鐨勮繍琛屾椂錛圧untime錛夌殑閰嶇疆
10:鍦╝ction綾諱腑鍙栧緱web涓嬫煇涓鏂囦歡澶圭墿鐞嗚礬寰勶紙緇濆璺緞錛夌殑鏂規硶
filePath = ServletActionContext.getServletContext().getRealPath("/upLoadFiles")
11錛氳鎯寵繑鍥炵殑欏甸潰涓嶆槸涓涓洿鎺SP欏甸潰鑰屾槸瑕佸厛閫氳繃榪斿洖action涓殑鏂規硶璇誨彇鐩稿簲鐨勬暟鎹啀榪斿洖鍒癹sp欏甸潰錛屾湁涓ょ鏂規硶
1錛夊湪struts.xml涓繖涔堣緗?
<result name="list" type="redirect-action">sysmanage/UserBaseInfoAction!findUserBaseInfo.action</result>
2錛夊湪action涓繑鍥炴椂鐩存帴璋冪敤榪欎釜鏂規硶鍗沖彲
return findList錛堬級錛?
12錛氳緗甤heckboxlist涓粯璁ゅ肩殑鏂規硶
<s:checkboxlist name="skills1"
="Skills 1"
list="{ 'Java', '.Net', 'RoR', 'PHP' }"
value="{ 'Java', '.Net' }" />
<s:checkboxlist name="skills2"
label="Skills 2"
list="#{ 1:'Java', 2: '.Net', 3: 'RoR', 4: 'PHP' }"
listKey="key"
listValue="value"
value="{ 1, 2, 3 }"/>
13錛氫簩綰х駭榪炰笅鎷夋
<s:set name="foobar"
value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" />
<s:doubleselect list="#foobar.keySet()"
doubleName="technology"
doubleList="#foobar[top]"
label="Technology" />