What the hell is Struts2?
Citing the introduction of Baidu Encyclopedia:Struts2 is a Web application framework based on the MVC design pattern, essentially equivalent to a servlet, in the MVC design pattern, Struts2 as a controller to establish the data interaction between the model and the view. .
Introduction completed ...
Its core schematic diagram online a lot, I made a simple change here, if there is not enough to explain the place, please correct me. Schematic please look:
All right, we're almost done here, let's talk about something useful. Struts2 How to use it, look at the following steps
1. Create a new Web project (as if it were nonsense)
2. Import the Struts2 jar package
3. Writing the Web. XML configuration
4. Writing Struts.xml Configuration
5. Write the Action class
6. Writing JSP pages
The first two steps are ignored, directly starting with the third step:
3. Introducing Struts2 in Web. XML (actually a filter)
12345678 |
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
|
4. Write the Struts.xml configuration (this file is placed in the SRC root directory)
123456 |
<
package
name
=
"default"
namespace
=
"/"
extends
=
"struts-default"
>
<!-- 指定Method调用 -->
<
action
name
=
"userLogin"
class
=
"com.pxy.action.Hello"
>
<
result
>/login.jsp</
result
>
</
action
>
</
package
>
|
5. Write the action class (put in the com.pxy.action package)
12345678 |
public
class Hello
extends
ActionSupport {
@Override
public
String execute()
throws
Exception {
System.out.println(
"默认调用的方法!"
);
return
SUCCESS;
}
}
|
6. Writing JSP pages
12345678 |
<
body
>
登录界面<
br
/>
<
form
action
=
""
method
=
"post"
>
账号:<
input
type
=
"text"
name
=
"loginid"
/><
br
/>
密码:<
input
type
=
"password"
name
=
"loginpwd"
/><
br
/>
<
input
type
=
"submit"
value
=
"登录"
/>
</
form
>
</
body
>
|
All the coding is done here, and then we enter the http://localhost:8080/strDemo/userLogin.action in the address bar to try the results. Normal access to the login.jsp page.
I modified the tomcat port to 8888.
Sir, you are satisfied.
If something goes wrong, it must be your posture is not right, get up and go to bed again.
If it is not right, then sleep a little more, wake up must be normal, please believe me ...
Java from getting started to giving up: a basic way to access Struts2