Function:
Click Hello to invoke the Execute function
Click Update to call the update function
1. Project structure
2.web.xml
<?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"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"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"> <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> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
3.useraction.java
Packagecom.action;ImportCom.opensymphony.xwork2.ActionSupport; Public classUseractionextendsActionsupport {Private Static Final Longserialversionuid=1l; PrivateString Info; PublicString GetInfo () {returninfo; } Public voidSetINfo (String info) { This. info=info; } PublicString Execute ()throwsexception{Info= "Hello World bb!"; return"Hello"; } PublicString Update ()throwsexception{Info= "ok,updated!"; return"Update"; }}
4.index.jsp
<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><%@ taglibprefix= "S"URI= "/struts-tags"%><!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>Insert Title here</title></Head><Body> <Center> <ahref= "User_execute">Hello</a><BR> <ahref= "User_update">Update</a> </Center></Body></HTML>
5.struts.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.or G/dtds/struts-2.0.dtd "><Struts> < Packagename= "Default"namespace="/"extends= "Struts-default"> <Actionname= "User_*"class= "Com.action.UserAction"Method= "{1}"> <resultname= "Hello">/hello.jsp</result> <resultname= "Update">/update.jsp</result> </Action> </ Package></Struts>
6.hello.jsp
<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><%@ taglibprefix= "S"URI= "/struts-tags"%><!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>Insert Title here</title></Head><Body> <Center> <S:propertyvalue= "Info" /> </Center></Body></HTML>
7.update.jsp
<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><%@ taglibprefix= "S"URI= "/struts-tags"%><!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>Insert Title here</title></Head><Body> <Center> <S:propertyvalue= "Info" /> </Center></Body></HTML>
DMI (Dynamic Method invocation)