I. Creating a MAVEN Project
Http://www.cnblogs.com/zhanqun/p/8425571.html
Ii. adding Struts2 Core dependency packages and other dependencies
Open the POM.XM Configuration interface
Click the Add button to add Struts2-core.jar
Select and click OK to add the entry to the project, and add the following after completion
Third, new action
1, the new package named Cn.smallbyte.mavendemo.action
2. Create a class named Useraction in the package, inheriting from Com.opensymphony.xwork2.ActionSupport
Useraction.java content is as follows
Packagecn.smallbyte.mavendemo.action;Importjava.io.UnsupportedEncodingException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.struts2.ServletActionContext;ImportCom.opensymphony.xwork2.ActionSupport; Public classUseractionextendsActionsupport {Private Static Final LongSerialversionuid = 1L; PublicString Execute () {returnSUCCESS; } PublicString Login () {Try{httpservletrequest Request=servletactioncontext.getrequest (); HttpServletResponse Response=Servletactioncontext.getresponse (); Request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8"); String username= Request.getparameter ("username"); String Password= Request.getparameter ("Password"); System.out.println ("Name->" + username + ",password->" +password); if("admin". Equals (username) && "123456". Equals (password)) { returnSUCCESS; } Else { return"Login"; } } Catch(unsupportedencodingexception e) {e.printstacktrace (); } returnSUCCESS; }}
3, configuration struts2 core configuration file (Src/main/resources under the Struts.xml file), the content is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts> <constantname= "Struts.enable.DynamicMethodInvocation"value= "false" /> <constantname= "Struts.devmode"value= "true" /> < Packagename= "Mavendemoaction"namespace= "/demo"extends= "Struts-default"> <Actionname= "Login"class= "Cn.smallbyte.mavendemo.action.UserAction"Method= "Login"> <resultname= "Success">/success.jsp</result> <resultname= "Login">/login.jsp</result> </Action> </ Package></Struts>
4, the configuration/src/main/webapp/web-inf/web.xml, the contents are as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0"> <Display-name>Mavendemo</Display-name> <!--struts2 - <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>*.action</Url-pattern> </filter-mapping></Web-app>
5. Create a Web page
SUCCESS.JSP/LOGIN.JSP two pages are configured in the Struts.xml file
Login.jsp as follows
<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Login Page</title></Head><Body> <formAction= "${pagecontext.request.contextpath}/demo/login.action"Method= "POST">Username:<inputtype= "text"name= "username"value="" /><BR/>Password:<inputtype= "text"name= "Password"value="" /> <BR/> <inputtype= "Submit"value= "Submit" /> </form></Body></HTML>
Success.jsp as follows
<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Login Success</title></Head><Body>Login Success</Body></HTML>
6. Compiling
Right-click the item, then select
Run AS-a Maven install until the following build success message indicates successful compilation
7. Release
Right-click Project, run as, run on Server, manually define a new server->tomcat v7.0 server
Click Finish to complete.
8. Verification
In the browser input: http://localhost:8080/mavendemo/demo/login.action
Enter admin/123456 after submit login success and jump to success.jsp page
Divert series---using MAVEN to create a Struts 2 project under Eclipse