默认Action,所有未找到路径显示error.jsp页面。
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/error.jsp</result>
</action>
package com.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class HelloWord extends ActionSupport implements ServletRequestAware, ServletResponseAware,ServletContextAware{ public String add(){ return "add"; } public String update(){ return "update"; } @Override public String execute() throws Exception{ System.out.println("执行action"); return SUCCESS; } @Override public void setServletResponse(HttpServletResponse response) { // TODO Auto-generated method stub } @Override public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub } @Override public void setServletContext(ServletContext context) { // TODO Auto-generated method stub } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index"></default-action-ref> <action name="index"> <result>/error.jsp</result> </action> <action name="helloword_*" method="{1}" class="com.action.HelloWord"> <result>/result.jsp</result> <result name="add">/{1}.jsp</result> <result name="update">/{1}.jsp</result> </action> </package> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> </struts>
时间: 2024-10-10 14:45:10