1 Overview of MVC ideas
2 download and installation of Struts 2
Add the jar of Struts 2 to the project Lib directory and modify the Web. xml file:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1"> <!--defining the Core Filter for Struts 2 - <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><!--Intercept all requests - </filter-mapping></Web-app>
Add struts.xml under src directory:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.a Pache.org/dtds/struts-2.3.dtd "><Struts> <constantname= "Struts.enable.DynamicMethodInvocation"value= "false"/> <constantname= "Struts.devmode"value= "true"/> <constantname= "Struts.custom.i18n.resources"value= "Mess"/><!--src/mess.properties - < Packagename= "Struts2"namespace="/"extends= "Struts-default"> <!--handles all user requests and renders the JSP file with the same name directly under/web-inf/content: Request: Http://localhost/loginForm access:/web-inf/content/loginform.jsp /c0> - <Actionname="*"> <result>web-inf/content/{1}.jsp</result> </Action> </ Package></Struts>
loginpage= Login page errorpage= error page Succpage= Success page Failtip= Login failed Succtip= welcome, {0} , login successfuluser = Username Pass= Password Login= login
<%--loginform.jsp--%><%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><%@ taglib Prefix="s"URI="/struts-tags" %><HTML><Head> <title><S:textname= "LoginPage"/></title></Head><Body><S:formAction= "Login"> <S:textfieldKey= "User"name= "username"/> <S:textfieldKey= "Pass"name= "Password"/> <S:submitKey= "Login"/></S:form></Body></HTML>
Public classLoginactionextendsActionsupport {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } @Override PublicString Execute ()throwsException {if(GetUserName (). Equals ("admin") && GetPassword (). Equals ("123456") {actioncontext.getcontext (). GetSession (). Put ("User", GetUserName ()); returnSUCCESS; } Else { returnERROR; } }}
In Struts.xml, add an action named "Login":
<Actionname= "Login"class= "Com.hundsun.action.LoginAction"> <resultname= "Success">/web-inf/content/welcome.jsp</result> <resultname= "Error">/web-inf/content/error.jsp</result></Action>
3 The process of Struts 2
4 General configuration of Struts 2
< lightweight Java EE Enterprise Application >2:struts 2 basic usage