Currently, many enterprises use frameworks for project development, and the same is true in Shanghai. Most enterprises use ssh and ssi, which is suitable for development by many people and easy to maintain. The following is the fastest way to build the struts2 development environment.
1. Download The struts2 Development Kit. This step is simple and I will not talk about it. The latest version is 2.2.31.
2. Copy the jar package used for development under lib. The required jar package is as follows:
If it is difficult to find out, you can copy all the jar packages without the end of plugin.
3. Configure the web. xml file and add a filter.
View plain
<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern> *. action </url-pattern>
<Url-pattern> *. jsp </url-pattern>
</Filter-mapping>
4. Compile the struts configuration file. Copy a template and modify it by yourself.
View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "struts2" extends = "struts-default">
<Action name = "HelloAction" class = "org. lxh. action. UserAction"> <! -- The class here uses the full name of the class -->
<Result name = "success">/index. jsp </result> <! -- The class here uses the full name of the class -->
</Action>
</Package>
</Struts>
You must note that extends = "struts-default" must be written. Otherwise, you will not be able to use many of the content in struts.
5. Write a struts action
View plain
Package org. lxh. action;
Public class UserAction {
Public String execute (){
Return "success ";
}
}
The method here is execute. By default, struts will call this method. The returned value "success" is the one in the struts configuration file.
6. Open the browser to access this action, and enter this address (http: // localhost: 8080/struts2/HelloAction. action) in the browser. The following page is displayed:
Below we also paste this simple jsp code
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
HelloWorld
</Body>
</Html>
Sometimes there is no prompt in the struts configuration file. This problem can be solved in this way.
<1> Find the struts-2.0.dtd In the downloaded struts package
<2> Based on Configuration
Click Add. The following dialog box is displayed. Select the file system and find the dtd file. After configuration, a prompt is displayed.
The code is almost finished here. I will talk about the use of the tag library tomorrow. Leave a message if you have any questions.
From Nothing is impossible's dream