第一个Struts框架

action类代码:

package Demo;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//处理请求
	public String execute() throws Exception {
    	System.out.println("访问到了action,正在处理请求");
    	System.out.println("调用了service");
    	return "success";
    }
	 
}

配置文件信息:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
  <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> 
  
  
</web-app>

配置文件信息:struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="xxxx" extends="struts-default">
    	<action name="hello" class="Demo.HelloAction" method="execute">
    		<result name="success">/success.jsp</result>
    	</action>
    </package> 
</struts>

jsp页面:success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  success.jsp <br>
  </body>
</html>



猜你喜欢

转载自blog.csdn.net/erchouchou/article/details/80639028