1. Create a struts WEB Project
Project name: struts2101407
2. Add struts2 support to the Project
Commons-fileupload-1.3.jar
Commons-io-2.0.1.jar
Commons-lang3-3.1.jar
Freemarker-2.3.19.jar
Javassist-3.11.0.GA.jar
Ognl-3.0.6.jar
Struts2-core-2.3.15.1.jar
Xwork-core-2.3.15.1.jar
3. Add the struts2 core configuration file
1. Create the conf directory in the project
2. Create a core configuration file in the conf directory
Core configuration file name: struts. xml
Core configuration file content:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.3 // en"
Http://struts.apache.org/dtds/struts-2.3.dtd>
<Struts>
</Struts>
4. Configure the struts core filter in the web. xml file
<Filter>
<Filter-Name> struts </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> struts </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
5. Create a controller
1. Create a package under the src directory
Package name: CN. jbit. struts2101401.web. Action
2. Create a controller under the package
Controller name: useraction. Java
Controller content:
Public class useraction extends actionsupport {
Private user;
Public String login (){
If ("123". Equals (user. GetUserName () & "123". Equals (user. GetPassword ())){
Return "success ";
}
Return Error;
}
// Omit getter and setter
}
6. Configure the controller (action) in the core configuration file struts. xml)
<Constant name = "struts. UI. Theme" value = "simple"> </constant>
<Package name = "default" namespace = "/" extends = "struts-Default">
<! -- Global -->
<Global-Results>
<Result name = "error">/WEB-INF/pages/error. jsp </result>
</Global-Results>
<! -- User action -->
<Action name = "useraction" class = "cn. jbit. struts2101401.web. Action. useraction">
<Result name = "success">/WEB-INF/pages/success. jsp </result>
<! -- Local -->
<Result name = "error">/WEB-INF/pages/error. jsp </result>
</Action>
</Package>
7. Create a view)
1. View-Logon
View name: index. jsp
View content:
<S: Form Action = "useraction! Success. Action "method =" Post ">
Username: <s: textfield name = "user. username"/> <br/>
Password: <s: password name = "user. Password"/> <br/>
<S: Submit value = "Logon"/>
</S: Form>
2. View-Logon successful
View name: success. jsp
View content:
Success. jsp
3. View-Logon Failed
View name: Error. jsp
View content:
Error. jsp
This article from the "Su Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1564330
Struts-global configuration