Eclipse搭建Strust2框架

奮鬥的小青年_發表於2016-10-09

1、建立一個web專案


2、Projcet name為“struts2Demo”,點選Finish


3、在“WebContent-WEB-INF-lib”下匯入struts2需要的jar包




4、在web.xml中配置struts2的過濾器



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<!-- Struts2的核心控制器 -->
	<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>


5、新建一個“struts2Demo” 包,並在包下建立“Struts2DemoAction”類


package struts2Demo;

import com.opensymphony.xwork2.ActionSupport;

public class Struts2DemoAction extends ActionSupport {
	
	public String HelloWorld()
	{
		System.out.println("Hello World");
		return SUCCESS;
	}
}

6、新建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>
	<!-- 開啟開發模式 -->
    <constant name="struts.devMode" value="true" />
    <package name="strutsDemo" namespace="/" extends="struts-default">
    	<!-- 配置一個動作 -->
    	<action name="HelloWorld" class="struts2Demo.Struts2DemoAction" method="HelloWorld">
    		<!-- 配置結果檢視 -->
    		<result name="success">/success.jsp</result>
    	</action>
    	
    </package>
	
</struts>

7、建立success.jsp


<%@ page language="java"  pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>title</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	哈哈,成功了
  </body>
</html>

8、將專案釋出到Tomcat,訪問。

http://localhost:8080/struts2Demo/strutsDemo/HelloWorld/HelloWorld


相關文章