xwork-2.0.4.jar 三、开始工作 在WebContent下建立index.jsp,建立方式如图。 index.jsp的内容如表,我们暂时不分析。 <%@ page contentType=\%>
Example by Doer Liu@UTStarcom sz This is my JSP page.
此时就可以运行该工程,忙了这么久,看看效果吧。 运行方式:右键点击index.jsp,选择Run/Debug As?Run on Server,在弹出窗口中默认我们使用的Tomcat Server,点击finish完成。可以看到eclipse中内嵌的浏览器显示我们的网页。其中表单的输入在我们的工程中将得到输入数据(用户名和密码),这些数据会传给我们将要建立的Action处理。 现在来看看如何建立我们的Action。在src下新建一个package(包)名为action用于保存响应Web请求的Action类。在action包下新建Action类LoginAction(action.LoginAction)如下,注意类的继承关系。 package action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; import org.springframework.web.struts.ActionSupport; //我们继承spring提供的Action衍生类org.springframework.web.struts.ActionSupport public class LoginAction extends ActionSupport{ } public ActionForward execute( } ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward(\); 但是现在index.jsp的内容怎么和LoginAction的数据匹配呢,我们看到LoginAction的execute方法有一个属性ActionForm,于是我们建立一个类forms.UserInfoForm如下,继承ActionForm。 package forms; import org.apache.struts.action.ActionForm; public class UserInfoForm extends ActionForm { } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } 有了两个头,又有了保持内容的类,现在看看我们如何用struts把他们联系起来吧。 现在需要在WEB-INF下建立文件struts-config.xml。其中form-beans定义了表单是如何映射的,这里用我们刚刚定义的forms.UserInfoForm。
在
中定义了我们的Action。它的属性attribute指出Action的内容输入是我们自定义的ActionForm,path给Action赋予一个路径,input指明只接受index.jsp的输入, Struts2+Spring2+Hibernate3 simple example by Doer Liu@UTstarcom xmlns=\ xmlns:xsi=\ xsi:schemaLocation=\ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\> encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 struts org.apache.struts2.dispatcher.FilterDispatcher struts /struts2spring2hib3bydoer/* /* org.springframework.web.context.ContextLoaderListener