飞舞的幻想

常用链接

统计

Google Adsense

Zanox FWHX

花个明白

最新评论

一个WebWork实现的简单登录过程以及遇到问题并附代码

1.login.jsp中点击submit按纽后,请求将被发送到web容器的login.action,
2.web容器检索web.xml,通过
<servlet-mapping>
        <servlet-name>webwork</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

这一段得到webwork关键字,再通过
    <servlet>
        <servlet-name>webwork</servlet-name>
        <servlet-class>
            com.opensymphony.webwork.dispatcher.ServletDispatcher
        </servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

将请求转发给com.opensymphony.webwork.dispatcher.ServletDispatcher类来处理,这个类会读取xwork.xml文件
3.通过和最初login.jsp提交的"login.action"中去掉web.xml中的定义的".action", 得到"login",在xwork.xml中的
        <action name="login" class="com.cdpgc.salary.LoginAction">
            <result name="failed" type="dispatcher">login-failed.jsp</result>
            <result name="success" type="dispatcher">login-success.jsp</result>
        </action>

找到"login"的定义,并寻找com.cdpgc.salary.LoginAction来处理这个请求,当该对象的execute()方法返回
<result>节点"name"属性定义的字符串的时候,将结果使用某个jsp来响应.这里是
success->login-success.jsp
failed->login-failed.jsp
4.两个jsp中使用了
<ww:property value="user" />

来访问处理类(com.cdpgc.salary.LoginAction)的属性,它依赖于
<%@ taglib uri="webwork" prefix="ww" %>

定义的标签库,而这个标签库定义中的uri依赖于web.xml中的
    <taglib>
        <taglib-uri>webwork</taglib-uri>
        <taglib-location>
            /WEB-INF/lib/webwork-2.1.7.jar
        </taglib-location>
    </taglib>

定义的taglib-uri名称

另外,在LoginAction.java中webwork会将发出请求的form中的值--------在<input type='xxx' name='xxx'>中的值
填入到自己的属性中(如果有同名属性的化),这样我们可以获得请求参数.在处理完毕后,同样可以设置它,
而在结果jsp中,webwork又会将该值提供给jsp访问,当然还是基于名称相同.

遇到问题
一、webwork jar文件添加,需要的Jar需要添加完备。
二、java.lang.NoSuchMethodError: javax.xml.transform.dom.DOMResult.getNextSibling()Lorg/w3c/dom/Node;
    org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:199)
    org.apache.xalan.transformer.TransformerIdentityImpl.setDocumentLocator(TransformerIdentityImpl.java:880)
    com.opensymphony.xwork.util.DomHelper$DOMBuilder.setDocumentLocator(DomHelper.java:233)
    com.opensymphony.xwork.util.location.LocationAttributes$Pipe.setDocumentLocator(LocationAttributes.java:302)
    com.opensymphony.xwork.util.DomHelper$StartHandler.setDocumentLocator(DomHelper.java:292)
    org.apache.xerces.parsers.AbstractSAXParser.startDocument(Unknown Source)
    org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source)
    org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source)
    org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source)
    org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    javax.xml.parsers.SAXParser.parse(Unknown Source)
    com.opensymphony.xwork.util.DomHelper.parse(DomHelper.java:122)
    com.opensymphony.xwork.config.providers.XmlConfigurationProvider.loadConfigurationFile(XmlConfigurationProvider.java:642)
    com.opensymphony.xwork.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:91)
    com.opensymphony.xwork.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:85)
    com.opensymphony.xwork.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:54)
    com.opensymphony.xwork.DefaultActionProxy.<init>(DefaultActionProxy.java:57)
    com.opensymphony.xwork.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:46)
    com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:216)
    com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:111)
   
问题原因原来是因为使用了tomcat5.0.28它里面的xml-api.jar把jdk1.5里面的DOMResult冲掉了 ,改用Tomcat的5.5版本问题解决。
三、javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found
javax.xml.transform.TransformerFactory.newInstance(Unknown Source)
com.opensymphony.xwork.util.DomHelper$DOMBuilder.<clinit>(DomHelper.java:168)
com.opensymphony.xwork.util.DomHelper.parse(DomHelper.java:116)
com.opensymphony.xwork.config.providers.XmlConfigurationProvider.loadConfigurationFile(XmlConfigurationProvider.java:642)
com.opensymphony.xwork.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:91)
com.opensymphony.xwork.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:85)
com.opensymphony.xwork.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:54)
com.opensymphony.xwork.DefaultActionProxy.<init>(DefaultActionProxy.java:57)
com.opensymphony.xwork.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:46)
com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:216)
com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:111)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
问题原因缺少xalan.jar文件,去http://xml.apache.org/xalan-j/index.html下载即可。

代码:login.jsp,loginsucess.jsp,loginfailed.jsp,LoginAction.java.
      我们登录login.jsp,输入用户名,post之后,请求将提交给LoginAction处理,如果成功则返回login-success.jsp,否则返回loginfailed.jsp
 
      login.jsp
<html>
<head>
<title>WebWork Tutorial - Lesson 3 - Example 1</title>
</head>

<body>

<p>
Click the [post] button below to Login.
</p>

<form action="login.action" method="post">
<p>user:<input type="text" name="user" value="" /></p>
<p>password:<input type="password" name="password" value="" /></p>
<p><input type="submit" value="post" /></p>
</form>

</body>
</html>

login-success.jsp

<%@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
<title>welcome to salary</title>
</head>
<body>

<font color="green">

<ww:property value="user" /> has logon [SalaryQueryModule]

</font>
<br>



</body>
</html>

login-failed.jsp

<%@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
<title>failed login</title>
</head>
<body>
<font color="red">
error login by [<ww:property value="user" /> ],invalid user or pwd
</font>
<br>
</body>
</html>

web.xml,位于web-inf目录下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>My WebWork Application</display-name>
    <servlet>
        <servlet-name>webwork</servlet-name>
        <servlet-class>
            com.opensymphony.webwork.dispatcher.ServletDispatcher
        </servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>webwork</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
   
    <taglib>
        <taglib-uri>webwork</taglib-uri>
        <taglib-location>
            /WEB-INF/lib/webwork-2.1.7.jar
        </taglib-location>
    </taglib>
</web-app>

xwork.xml位于web-inf\classes目录下
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>
    <!-- Include webwork defaults (from WebWork JAR). -->
    <include file="webwork-default.xml" />
   
    <!-- Configuration for the default package. -->
    <package name="default" extends="webwork-default">
        <!-- Default interceptor stack. -->
        <default-interceptor-ref name="defaultStack" />
       
        <!-- Action: Lesson 03: HelloAction. -->
        <action name="login" class="com.cdpgc.salary.LoginAction">
            <result name="failed" type="dispatcher">login-failed.jsp</result>
            <result name="success" type="dispatcher">login-success.jsp</result>
        </action>
    </package>
</xwork>

LoginAction.java
 1/**//*
 2 * Created on 2005-8-4
 3 *
 4 * TODO To change the template for this generated file go to
 5 * Window - Preferences - Java - Code Style - Code Templates
 6 */
 7package com.cdpgc.salary;
 8
 9import com.opensymphony.xwork.ActionSupport;
10
11/**//**
12 * @author codemyth
13 *
14 * TODO To change the template for this generated type comment go to Window -
15 * Preferences - Java - Code Style - Code Templates
16 */
17public class LoginAction extends ActionSupport {
18
19    String user;
20    String password;
21
22    /**//*
23     * (non-Javadoc)
24     *
25     * @see com.opensymphony.xwork.Action#execute()
26     */
27    public String execute() throws Exception {
28        // TODO verify user-password
29        if (user.equals("bbb"))
30            return super.SUCCESS;
31        else
32            return "failed";
33    }
34    /**//**
35     * @return Returns the user.
36     */
37    public String getUser() {
38        return user;
39    }
40    /**//**
41     * @param user
42     *            The user to set.
43     */
44    public void setUser(String user) {
45        this.user = user;
46    }
47    /**//**
48     * @return Returns the password.
49     */
50    public String getPassword() {
51        return password;
52    }
53    /**//**
54     * @param password
55     *            The password to set.
56     */
57    public void setPassword(String password) {
58        this.password = password;
59    }
60}
文章大部分内容来自http://www.blogjava.net/codemyth/archive/2005/08/06/9453.html

posted on 2006-12-06 14:39 飞舞的幻想 阅读(1222) 评论(1)  编辑  收藏 所属分类: 框架学习

评论

# re: 一个WebWork实现的简单登录过程以及遇到问题并附代码 2009-06-18 10:50 tttlan

你好,最近在学习webwork,在运行第一个 helloworld例子除了一个错误:

file:/F:/webwork/webapps/WEB-INF/classes/xwork.xml:13:61
com.opensymphony.xwork.config.providers.XmlConfigurationProvider.loadConfigurationFile(XmlConfigurationProvider.java:673)
com.opensymphony.xwork.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:91)
com.opensymphony.xwork.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:159)
com.opensymphony.xwork.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
com.opensymphony.xwork.DefaultActionProxy.<init>(DefaultActionProxy.java:60)
com.opensymphony.xwork.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:46)
com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:265)
com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:111)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

也没说什么错误,我估计是加载 xwork。xml时候出错,可又找不出哪儿错了。麻烦你帮忙看一下。

谢谢了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: