WebWork 2 : 常见问题解答
本页由lagcisco最后修改2005-3-2于2004年10月18日.
值栈中的缺省值是什么? (使用#foo访问) cvs -d :pserver:guest@cvs.dev.java.net:/cvs login (Use an empty password, just hit enter..) cvs -d :pserver:guest@cvs.dev.java.net:/cvs checkout webwork cvs -d :pserver:guest@cvs.dev.java.net:/cvs checkout xwork Note: WebWork from the CVS does not compile with the latest 1.5 J2sdk. Use the stable J2sdk 1.4.2. How do I build the latest versions XWork and Webwork?Just go into the XWork or WebWork directories and run 'ant' (you must have ant installed and have the jars of junit and clover inside $ANT_HOME/lib) Once you have built the xwork.jar copy it into the webwork/lib/core folder, and delete the old one. How do I use messages from within the validator?<validators> <field name="name"> <field-validator type="requiredstring"> <message key="template.name.errors.required">A default message in case the key is not found</message> </field-validator> </field> </validators> How do I set a global resource bundle? In webwork.properties(as of Webwork 2.1.1),you can now use: webwork.custom.i18n.resources=global-messages Serveral resource bundles can be specified by comma separating them. for example see webwork.properties : http://wiki.opensymphony.com/display/WW/webwork.properties Java class (thanks Drew McAuliffe): public class WebworkGlobalMessagesListener implements ServletContextListener { private static Logger log = Logger.getLogger(WebworkGlobalMessagesListener.class); private static final String DEFAULT_RESOURCE = "global-messages"; /** * Uses the LocalizedTextUtil to load messages from the global message bundle. * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.Servle tContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { log.info("Loading global messages from " + DEFAULT_RESOURCE); LocalizedTextUtil.addDefaultResourceBundle(DEFAULT_RESOURCE); log.info("Global messages loaded."); } /** * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { // do nothing } } web.xml: (under listeners section) <listener> <listener-class>mypackagename.WebworkGlobalMessagesListener</listener-class> </listener> You need to create a message for that field, for example if you have a user.dob field you would use this in your messages file (see above for example on setting a global messages file): invalid.fieldvalue.user.dob=Please enter Date of Birth in the correct format. How do I get access to the Session? ActionContext.getParameters() (returns Map, works internally using a ThreadLocal) How can I get the HttpServletRequest? Obtain the ComponentManager from the request: ComponentManager cm = (ComponentManager) ServletActionContext.getRequest().getAttribute("DefaultComponentManager"); then you need to initialize it using: cm.initializeObject(Object) How do I decouple XWork LocalizedTextUtil global resource bundle loading from serlvets (ServletContextListener)? If i have : #tag(Select "label='xxx '" "name='xxx'" "list=?") or #tag(combobox "label='Prioridade'" "name='inavis.avisTpPrioridade'" "list=?") the values in this combobox, what i need to do? Exemple: html tag i use to do:
<select..>
<otpion value="" selected>XXX</option>
</selct>
so...i need to do this using Webwork tags from Velocity...how can i do this?? How do I add I18N to a UI tag, like ww:textfield? <ww:textfield label="'i18n.label'" name="'label1'" value="''"> This will get the localized text message for the key "i18n.label" and put it in the label. <ww:textfield label="getText('i18n.label')" name="'label1'" value="''"> Can I add I18N outside the Action's context? i.e. adding i18n to some JSP using the ww taglib? Sure, that's what the <include> element is for. Most xwork.xml files already have one: <xwork> <include file="webwork-default.xml"/> <include file="config-browser.xml"/> <package name="default" extends="webwork-default"> .... </package> <include file="other.xml"/> </xwork> This tells it to load the webwork-default.xml from the webwork jar file <package> elements… They will be loaded in the same order as it reads from top to bottom and adds things as it reads them. How can I put a String literal in a Javascript call, for instance in an onChange attribute? The problem is in escaping quotes and getting the double quotes around the final value, like we expect in HTML attributes. Here's an example of the right way to do this (thanks to John Brad):onchange='"someFunc(this.form, \'abc\')"'
Notice here that there are single quotes surrounding the double quotes, and then the single quotes inline in the Javascript are escaped. This produces this result: onchange="someFunc(this.form, 'abc')"
Why won't the 'if' tag evaluate a one char string? <ww:if test="#myObj.myString == 'A'"> Why doesn't this work when myString is equal to A? </ww:if> <ww:if test='#myObj.myString == "A"'> This works! </ww:if> Alternatively, you can escape the double quotes in the String: <ww:if test="#myObj.myString == \"A\""> This works! </ww:if> I'm trying to run the webwork example in the tutorial on Tomcat, and it can't instantiate the VelocityEngine Tomcat says:javax.servlet.ServletException: Servlet.init() for servlet webwork threw exception at at com.opensymphony.webwork.views.velocity.VelocityManager.newVelocityEngine(VelocityManager.java:333) at com.opensymphony.webwork.views.velocity.VelocityManager.init(VelocityManager.java:146) at com.opensymphony.webwork.dispatcher.ServletDispatcher.init(ServletDispatcher.java:177) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:935) Solution: (thanks to Keith Lea) It turns out Velocity's Avalon logging system was trying to write to my tomcat folder.So that it's on file somewhere for other people, I will describe the solution: I created a file "velocity.properties" and placed it in my WEB-INF/classes folder. Inside the file I wrote:runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogSystem This stops velocity from logging, and makes webwork work again. |
Document generated by Confluence on Dec 14, 2004 16:36 |