个人学习时的小总结,希望对大家有帮助。
1、 新建一个项目:File->New ->Other…->MyEclipse ->Java Enterprise Project->Web Project->Next,输入项目的名称:StrutsLoginDemo,再点击Finish即可。
2、 右击项目名称StrutsLoginDemo->MyEclipse->Add Struts Capabilities…在出现的窗口中,很多都已经默认填写好了,只有Base package for new classes是可以进行更改的,我将其改为:com.mystruts,点击Finish完成。
3、 建立Struts的任一个组成部分(如:Action等),有以下三种方法:(此处是新建一个Action)
a) File > New > Other... > MyEclipse> Web-Struts > Struts 1.1 (or 1.0 or 1.2 )选择其下面列表中的即可新建。
b) 打开该项目的Struts配置文件struts-config.xml.。在界面的右部会出现“Outline”的小窗口,选择其列表下的“action-mappings”,右击选择“New Action”
c) 打开该项目的Struts配置文件struts-config.xml.使其显示他的“Design”界面,其中有一个工具条,上面列出的Struts包含的一些内容,你可以单击你想创建的对象的按钮,即可创建。
4、 下面是Building the Application,在该项目的Struts配置文件struts-config.xml.的“Design”界面,我们选择其工具条上的“J”,创建一个新的JSP页面,在弹出的窗口中填写“File Name”为“userLoginSuccess.jsp”,并且确保选择了“"Standard JSP using Struts 1.1"”,点击完成即可。这样MyEclipse就会自动创建一个对于的JSP编码界面。修改该JSP的代码为::
page language= "java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<title>My Struts 'userLoginSuccess.jsp' ending page</title>
</head>
<body>
Hello <bean:write name="userName" scope="request" />, you successfully logged in!
</body>
</html:html>
5、 Now we still need to create the userLogin.jsp page, the ActionForm and the Action.
在struts-config.xml的Design界面的空白处,右击选择“New”->“Form,Action and JSP”,在弹出的窗口中,在“Use Case”处填写名字“userLogin”其他的系统都会自动帮你完成。在下面的“Form Properties”选项卡中,点击“Add”按钮,如下:
在“JSP”选项卡中,勾选“Create JSP Form”。在“Methods”的选项卡中,确保所有的都不选。点击 “Next”。
6、在弹出的窗口中,所有的都差不多系统已经帮你填好了。在下部分,选择“Forwards”选项卡,点击“Add”按钮,设置Action Forwards,如下:
回到配置界面你会看到变化。
7、此时,此界面中双击“userLogin”小图片,出现其代码设计窗口,将其代码该为:
UserLoginAction.java
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserLoginForm userLoginForm = (UserLoginForm) form;
if(userLoginForm.getUserName().equals("myeclipse") && userLoginForm.getPassword().equals("myeclipse"))
{
request.setAttribute("userName", userLoginForm.getUserName());
return mapping.findForward("success");
}
return mapping.findForward("failure"); }
8、运行该Demo。首先部署,然后启动服务器Tomcat,在打开浏览器,或者是打开“Window > Show View >Web Browser”,
在地址栏输入: http://localhost:8080/StrutsLoginDemo/userLogin.jsp即可。
小技巧:
右击项目名称“StrutsLoginDemo” -> “Properties” -> 在弹出窗口选择“MyEclipse” ->“Web” -> 选择“Context Root”选项卡,在文本框中输入你要修改的名称(如“/abc”)。之后重启服务器Tomcat,重新部署。
在地址栏输入:http://localhost:8080/abc/userLogin.jsp即可访问。