这里来讲解一下strtus2动态配置的用法。
配置之后不用通过 <action method="">去配置调用的具体方法。
第一:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name> <filter>
<filter-name>struts2</filter-name>
<!-- 可在引用的jar包struts2-core-2.3.15.3.jar下的 org.apache.struts2.dispatcher.ng.filter找到-->
<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> </web-app>
第二:struts.xml
<package name="Menu" namespace="/Menu" extends="struts-default">
<action name="mainpage" class="com.almostman.action.MainPageAction">
<result name="success">/success.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package>
第三:MainPageAction.java
package com.almostman.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; public class MainPageAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; public String add() throws Exception { if("admin".equals(username) && "admin".equals(password)){
return SUCCESS;
} return INPUT;
} private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
这样就可以配置了
使用链接:http://localhost:8081/struts013/Menu/mainpage!add.action?username=admin&password=admin 就可以看到效果了。