Get Struts2 to settle in NetBeans 6-use Struts2 in NetBeans 6

Source: Internet
Author: User
Tags tomcat netbeans

Let Struts2 settle down in NetBeans 6.
--Use Struts2 in NetBeans 6

Foreword: NetBeans 6 and Struts2
Each time you use one of the Java editors or Ides (such as Eclipse), you'll always find yourself knocking code in NetBeans for one hours. As for Struts2, this is an MVC-based Web development framework that blends webwork and struts1.x, and is used fairly extensively.

In the current NetBeans6.1 you can not find the shadow of Struts2 (at least in the version I am using, it is said that someone has been developing plug-ins). However, this does not prevent Netbean 6 and Struts2 from walking together. In the back you will find that they are compatible with the very tacit.

This article is meant to show you how to bring NetBeans and Struts2 together (where Struts2 can even be used with JSF and visual Web packs)

Preparatory work:

1. JDK 5.0 (or later);

2. NetBeans 6 or NetBeans 6.1 Beta (this article uses NetBeans 6.1 beta);

3. STRUTS2 development Package;

4. Tomcat 5.5 (or later).

A Start NetBeans 6.1 To create a Web project


Create a Web Project:

Click Fileànew Project;

Select the web in categories, and then select the Web application in projects;

Click Next.

The following figure is filled in, where the server can also be NetBeans-bound Tomcat 6

Click Next and then click Next to see the following interface:


Don't choose anything. Click Finish. The project was created and entered the next section.


Two Create the Struts2 Library and import the STRUTS2 development Kit
Click Toolsàlibraries

Click on the new Library ...

Library name:struts2

Library Type:class Library

Click OK

Select the Struts2 Java package shown in the figure

Click OK to return to the main screen. Right-click Libraries and select Add Library ...


Click Add Library Basic STRUTS2 development package has been imported.


Three Configure Web.xml
Click Web.xml and click on the filters on the right to click Add Filter Element, as shown in the figure


Filter Name: You can fill in any of the long filter classes that are not easy to remember. Only a pity to open the browse ... Afterwards also cannot choose libraries inside of bag, cannot but say is a regret. Hopefully the next version of NetBeans will fix this little shortcoming.

Filter NAME:STRUTS2 filter;

Filter Class:org.apache.struts2.dispatcher.FilterDispatcher

Next is mapping filter, click Add ...


As shown in the picture, Struts2 Filter should be consistent with the above. URL pattern inside fill in *.action can


In fact, when you configure Web.xml in other Ides, you might want to write XML yourself (believe that most programmers hate writing long, smelly XML. And a little error, can look at the pain)

For the above steps, NetBeans automatically generates the following code for us, you can also go and see.


Now you can stop web.xml for a while.


Four Write Struts.xml
Struts.xml can be said to be the center of the entire STRUTS2 framework. Most configuration errors also appear on the Struts.xml. When a JSP page is submitted, it is not passed directly to another page as the traditional approach, but is referred to struts.xml for processing. Struts.xml calls the background action class for processing and then decides to turn to that page. So how do we write and where do we put struts.xml?

We want to place the Struts.xml under the default package package of source packages. As shown in figure



To name a file as struts


Next click Struts.xml to write, before we want to add in the header of the XML document

<! DOCTYPE Struts Public
"-//apache Software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >

Put the original <root></root> delete. Replace <struts></struts> enter "<p" and press Ctrl+space

This is when NetBeans has a full understanding of Struts2 (it's estimated they've become good partners). Continue to write, if you are afraid of error, or do not want too much trouble, press Ctrl+space. Netbeans 6.1 will solve a lot of problems for you. (It has to be said that later versions of the NetBean6.0 have a great improvement in code completion, both speed and intelligence are commendable) all the way to write down. Here's the coded code:


<struts>
<package name= "Xhinker" extends= "Struts-default" >
<action name= "Hello" class= "cn.xhinker.struts2.HelloAction" >
<result>/helloPage.jsp</result>
</action>
</package>
</struts>

The name attribute in the action label in the document should be paid special attention because it is used to identify the action. In the class attribute, the action class. When you click on a JSP page in the <a href= "Hello.action" >enter</a>. The program will find name= "Hello" tags in struts.xml according to Hello.action. Then find the background of the Helloaction class. Then according to the return value of the Helloaction instance, find the corresponding page in the result label, and realize the transfer of the page. The result label has a name property such as <result name= "Success" >/helloPage.jsp</result> if the Name property is not written, the default is Name= "Success".

Save, close this page, and then we'll write Helloaction


Five, write Helloaction class
Right-click the source Package to create a Java class.

Fill in the following:

Let helloaction inherit Actionsupport


Package cn.xhinker.struts2;
Import Com.opensymphony.xwork2.ActionSupport;
public class Helloaction extends Actionsupport ... {
}


This time someone would ask, how can I get NetBeans to help me override (rewrite) the parent class? No such a button? No need to find it. Ctrl+space. You want to rewrite the method, sorta has been shown in front of you:

All we need is this execute () method. A slight change in the code looks like this:


Public String Execute () throws Exception ... {
return this. SUCCESS;
}

Add a String variable msg then let NetBeans automatically generate the setter and getter methods. As shown in the pop-up menu, select Encapsulate Field ... Can


When the build is complete, the class is ready, and the complete code is:

Package cn.xhinker.struts2;
Import Com.opensymphony.xwork2.ActionSupport;
public class Helloaction extends Actionsupport ... {
Private String msg= "Hello World";
Public String Execute () throws Exception ... {
return this. SUCCESS;
}

Public String getmsg () ... {
return msg;
}

public void Setmsg (String msg) ... {
this.msg = msg;
}
}

into the next section.

Six Writing JSP pages

Add <a href= "hello.action" to the body tag in index.jsp >hello</a>


Create a new hello.jsp page to add the code as shown in the figure:

So far, you should start the tabby cat Tomcat. Buildàrun

I hope you didn't get into trouble:-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.