Let us check an illustration model class, Action, host page, and mapping.

By promoting these ponents, we're breaking up the work movement into three famous issues: the scene, the unit, and operator. Breaking up issues makes it easier to handle applications as they bee considerably plex.

If you want, turn up your own Java IDE, and enter the code even as we go.

This guide thinks youve pleted the How To Create A Struts 2 internet software information while having an operating standard Struts task. The instance code because of this information, helloworld, is obtainable for checkout from Struts 2 GitHub repository at struts-examples. The example works make use of Maven to deal with the artifact dependencies and build the .war records.

The Rule

Helps modify the basic-struts job to include the annotated following:

  • a product class to keep all of our information
  • a see that shows the content
  • an Action class to act due to the fact controller
  • a setup that ties anything along

The Struts 2 user subscriber list is a superb spot to become help. If you find yourself having an issue acquiring this software to your workplace look the Struts 2 mailing list. In the event that you dont discover a solution to your difficulties, article a concern on email list.

Step one - Produce The Unit Course MessageStore.java

Create the MessageStore class in src/main/java . Definitely note the bundle report below.

Keep in mind that inside the code found below the JavaDoc ments are omitted. In the grab instance, JavaDoc ments come.

MessageStore.java

Note the utilization of people getter method to enable accessibility the private information String characteristic. The Struts 2 framework requires that things you want to show into see ( HelloWorld.jsp ) proceed with the JavaBean-style conventions.

Step 2 - Produce The Action Lessons HelloWorldAction.java

We need a motion course to behave given that Controller. The experience course responds to a person actions (within this instance that motion might be pressing an HTML link and delivering a particular Address towards the Servlet container). Several of Action classs techniques were executed and a String result is came back. Using the property value the end result, a certain view web page (in this sample that view web page try HelloWorld.jsp ) try made.

Note the package and significance comments here.

HelloWorldAction.java

The Struts 2 framework can establish an item associated with the HelloWorldAction lessons and name the perform process responding to a customers action (simply clicking a web link that delivers a particular URL into the Servlet container).

Within this sample, the perform strategy brings an item of lessons MessageStore and then return the String constant ACHIEVEMENT .

Note furthermore anyone getter means for the private MessageStore item. Since you want to make MessageStore В object open to the scene page, HelloWorld.jsp ,В we should instead proceed with the JavaBean-style of supplying getter and setter practices where needed.

3 - Produce The See HelloWorld.jsp

We need a host web page to present the content definitely kept in the unit class MessageStore . Create the under JSP in folder.

The taglib directive tells the Servlet container that this page might be making use of the Struts 2 labels and therefore these labels should be preceded by an s .

The В label showcases the worth returned by contacting the strategy getMessageStore В for the HelloWorldAction В control lessons. That technique return a MessageStore В object. With the addition of the .message В onto the messageStore part of the appreciate attribute the audience is informing the Struts 2 structure to contact the getMessage В method of that MessageStore В item. The getMessage В approach to course MessageStore profits a String. It really is that String which will be presented because of the В tag.

Well discover more about tags within the next information. Start to see the Struts labels for more information about labels.

Step four - put The Struts setup In struts.xml

We are in need of a mapping to connect the Address, the HelloWorldAction В lessons (operator), and the HelloWorld.jsp В (the scene) with each other. The mapping informs the Struts 2 framework which class will reply to the customers actions (the URL), which way of that course would be performed, and exactly what see to give on the basis of the String consequences that process returns.

Change the struts.xml file (inside the Mvn task that document is within the src/main/resources folder) to include the action mapping. Position the action node (actions title="hello" ) between the beginning and shutting package node, soon after the experience mapping with the identity="index" . Your own plete struts.xml will want to look like:

struts.xml

Move 5 - Produce The URL Activity

Let us add an actions Address inside directory.jsp (read src/main/webapp folder) so the individual can click a web link to inform the Struts 2 framework to perform the execute technique of the HelloWorldAction lessons and render the HelloWorld.jsp see.

Initial put the taglib directive on top of the jsp . Further mix this p label

after the title label. Your brand new directory.jsp will want to look like:

index.jsp

The Struts url tag produces the URL with an action of hi. The hello action got mapped on HelloWorldAction lessons and its particular execute process. Whenever the consumer clicks about overhead URL it will probably result in the Struts 2 platform to run the execute way of the HelloWorldAction course. Next method returns the String triumph (continual ACHIEVEMENTS ), the view webpage HelloWorld.jsp are going to be made.

Step 6 - Build the combat File and Run the application form

Execute mvn jetty:run to perform the application.

Click the Hello community back link and you should get the HelloWorld

The Code Works

  1. The container receives from the web server an ask for the source hello.action . According to the setup loaded through the web.xml, the container locates that most needs are increasingly being routed to org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter , such as the *.action requests. The Struts Prepare And perform filtration may be the entry point to the structure.
  2. The framework searches for an activity mapping known as hello, and it also locates that the mapping corresponds to the category Hello World actions . The framework instantiates the experience and calls the Actions execute approach.
  3. The perform way produces the MessageStore object and profits VICTORY (= "success" ). The framework checks the action mapping observe exactly what web page to stream if PROFITS try came back. The structure informs the container to render while the reaction to the request, the reference HelloWorld.jsp .
  4. As web page HelloWorld.jsp will be prepared, the label phone calls the getter getMessageStore of HelloWorld motion after which phone calls the getMessage associated with the paardensport dating sites MessageStore object came back by getMessageStore . The label merges the worth of the content feature in to the responses.

What to recall

The platform utilizes behavior to endeavor HTML types as well as other needs. The experience class returns a result-name including ACHIEVEMENTS , ERROR or INPUT . On the basis of the mappings filled from struts.xml , confirmed result-name may select a web page (like in this example), another actions, or other internet reference (image, PDF).

No hay comentarios.

Agregar comentario