I've learned the basic usage of struts 2 before, and then I'll talk about the three access methods of struts 2.
The first way: specify the method attribute
The second way: dynamic method invocation (exclamation mark mode, need to turn on the corresponding switch), the official network is not recommended to use.
The third way: wildcard mode, recommended by the official website
No nonsense, code!!!
1. Specify the method attribute
1.1) Modify the Action class to add CURD internally
public class Hello extends ActionSupport { // Add to public String add() { System.out.println("Called the added method!"); return "add"; } // delete public String delete() { System.out.println("Called the deletion method!"); return "delete"; } // To update public String update() { System.out.println("Called the updated method!"); return "update"; } // query public String select() { System.out.println("Called the query method!"); return "select"; } }
1.2) Modify the struts.xml file (configure the corresponding Action)
<package name="default" namespace="/" extends="struts-default"> <action name="singerAdd" class="com.pxy.action.Hello" method="add"> <result name="add">/WEB-INF/jsp/singer_add.jsp</result> </action> <action name="singerDelete" class="com.pxy.action.Hello" method="delete"> <result name="delete">/WEB-INF/jsp/singer_delete.jsp</result> </action> <action name="singerUpdate" class="com.pxy.action.Hello" method="update"> <result name="update">/WEB-INF/jsp/singer_update.jsp</result> </action> <action name="singerSelect" class="com.pxy.action.Hello" method="select"> <result name="select">/WEB-INF/jsp/singer_select.jsp</result> </action> </package>
1.3) Add the corresponding JSP page, I put the corresponding JSP page in the / WEB-INF/jsp / directory.
1.4) Next you can access it at the following address.
http://localhost:8080/strDemo/singerAdd.action
http://localhost:8080/strDemo/singerDelete.action
http://localhost:8080/strDemo/singerUpdate.action
http://localhost:8080/strDemo/singerSelect.action
1.5) It's over here. If your program can't jump to the corresponding JSP page, please delete the project and do it again. If you can't do it 10 times, you can give it up directly.
2. Dynamic method
2.1) Modify the struts.xml file to set constants that open dynamic methods (recommended on the package tag)
<!-- Turn on dynamic method calls --> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
2.2) Modify the struts.xml file (configure the corresponding Action)
<!-- Dynamic method invocation --> <action name="singermng" class="com.pxy.action.Hello"> <result name="add">/WEB-INF/jsp/singer_add.jsp</result> <result name="delete">/WEB-INF/jsp/singer_delete.jsp</result> <result name="update">/WEB-INF/jsp/singer_update.jsp</result> <result name="select">/WEB-INF/jsp/singer_select.jsp</result> </action>
2.3) Access via the following address (note that the red part is the method name in the Action class)
http://localhost:8080/strDemo/singermng!add.action
http://localhost:8080/strDemo/singermng!delete.action
http://localhost:8080/strDemo/singermng!update.action
http://localhost:8080/strDemo/singermng!select.action
3. wildcard mode
3.1) Modify the struts.xml file (configure the corresponding Action, where * is a wildcard, you can set up more than one, then use {1} {2} to match in left-to-right order)
<!-- Wildcard mode call --> <action name="smng_*" class="com.pxy.action.Hello" method="{1}"> <result name="{1}">/WEB-INF/jsp/singer_{1}.jsp</result> </action>
3.2) Access via the following address
http://localhost:8080/strDemo/smng_add.action
http://localhost:8080/strDemo/smng_delete.action
http://localhost:8080/strDemo/smng_update.action
http://localhost:8080/strDemo/smng_select.action
The three basic access modes here are OK.
Ladies and gentlemen, if you are satisfied, please give me a compliment.···