Chapter 2 Call Javabean through Action
Add logic: Notpresent tag
Open the StrutSdemo1 project created in the previous section. Change hello.jsp to:
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Taglib Uri = "/ Tags / Struts-Bean" prefix = "bean"%>
<% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%>
<% @ Taglib Uri = "/ Tags / Struts-Logic" prefix = "logic"%>
NOT Find Stubean.
logic: NOTPRESENT>
body>
html: html>
The above black body part means looking for objects named Stubean in the "Request" range, and if you can't find it, you can display "zhf - not find stubean.". Because we didn't pass any request to hello.jsp, the "zhf - not find stubean" will now be accessed now.
Add a javabean called StudentBean
Package hello;
Public class studentbean {
Private string name = NULL;
Public string getname () {
Return Name;
}
Public void setname (String name) {
THIS.NAME = Name;
}
}
Add logic: Present tag
Change hello.jsp to:
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Taglib Uri = "/ Tags / Struts-Bean" prefix = "bean"%>
<% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%>
<% @ Taglib Uri = "/ Tags / Struts-Logic" prefix = "logic"%>
NOT Find Stubean.
logic: NOTPRESENT>
The Student Name:
logic: present>
body>
html: html>
The above black body part means that its Name property is displayed if an object called Stubean is found within the Request range. Of course, since we have not passed the Stubean object to Hello.jsp, "ZHF - Not Find Stubean." Will be displayed if you visit Hello.jsp.
Add a Action called HelloAction
First, use Eclipse's menu "Project | Properties | Java Build Path | Libraries", press the "Add Extenal Jars ..." button, add "C: / Javasoft / Tomcat 5.5 / Common / lib / servlet-api.jar" and "C : /Javasoft/struts/jakarta-struts 1.2.4/lib/ struts.jar. Then, create a category:
Package hello;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Apache.struts.action. *;
Public class helloaction extends action {
Public ActionForward Execute (ActionMapping MappingArg, Actionform Formarg,
HttpservletRequest Requestarg, httpservletResponse resqonsear) THROWS Exception {
// Having Passed The Validate
StudentBean JCL = New StudentBean ();
JCl.setname ("JCL");
Requestarg.setttribute ("Stubean", JCL);
Return mappingARG.FINDFORWARD ("Hello.jsp");
}
}
The Struts-Config.xml is then changed to:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.2 // En"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
global-forwards>
Path = "/ helloaction" TYPE = "Hello.HelloAction" Scope = "request" Validate = "True" Input = "/ hello.jsp" /> action-mappings> Struts-Config> Note that the order in the label cannot be reversed. Struts application, then accessed "http://127.0.0.1:9111/strutsdemo1/helloaction.do", you will see "The Student Name: JCL" page. See below: