web.xml

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  7.   <display-name></display-name>   
  8.       <filter> 
  9.         <filter-name>struts2</filter-name> 
  10.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  11.     </filter> 
  12.  
  13.     <filter-mapping> 
  14.         <filter-name>struts2</filter-name> 
  15.         <url-pattern>/*</url-pattern> 
  16.     </filter-mapping> 
  17.  
  18.  
  19.   <welcome-file-list> 
  20.     <welcome-file>index.jsp</welcome-file> 
  21.   </welcome-file-list> 
  22. </web-app> 

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.  
  6. <struts> 
  7.     <!-- 开启动态方法调用 --> 
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
  9.     <!-- 开启开发模式 --> 
  10.     <constant name="struts.devMode" value="true" /> 
  11.       
  12.     <!-- namespace="/"代表根命名空间 ,如果省略namespace 属性代表默认空间 --> 
  13.     <package name="default" namespace="/" extends="struts-default"> 
  14.         <action name="testaction" class="com.action.TestAction"> 
  15.             <result >/1.jsp</result> 
  16.         </action> 
  17.     </package> 
  18.  
  19.     <!-- Add packages here --> 
  20.     <!--<include file="example.xml"/> --> 
  21. </struts> 

com.action.TestAction

  1. package com.action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class TestAction extends ActionSupport {  
  6.  
  7.     @Override 
  8.     public String execute() throws Exception {  
  9.         // TODO Auto-generated method stub  
  10.         System.out.println("测试");  
  11.         return super.execute();  
  12.     }  

index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %> 
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.       
  12.     <title>跳转测试</title> 
  13.       
  14.     <meta http-equiv="pragma" content="no-cache"> 
  15.     <meta http-equiv="cache-control" content="no-cache"> 
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  18.     <meta http-equiv="description" content="This is my page"> 
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     --> 
  22.  
  23.   </head> 
  24.     
  25.   <body> 
  26.     
  27.     <form action="testaction"> 
  28.         <input type="submit" value="跳转"/> 
  29.     </form> 
  30.   </body> 
  31. </html> 

1.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %> 
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.     <title>跳转成功</title> 
  12.   </head> 
  13.   <body> 
  14.         跳转成功  
  15.   </body> 
  16. </html>