struts學習之HelloWorld

下載struts:

點擊打開鏈接

http://struts.apache.org/download.cgi


1、在java EE下新建dynamical web project工程

2、拷貝jar包到WEB-INF lib文件夾下
jar包爲D:\Jdk-eclipsejee-tomcat-ant\struts\struts-2.3.24.1-all\struts-2.3.24.1\apps 中的struts2-blank.war,WEB-INF lib文件夾中。
3、web.xml文件添加核心過濾器,做爲前端控制器
  <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>

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test0323</display-name>
  
  <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>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>

  </welcome-file-list>
</web-app>


4、添加struts.xml文件,默認放在WEB-INF/classes路徑下
<?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">
		<action name="hello" class="HelloAction">
			<result>/index.jsp</result>
		</action>
    </package>
    
    <!-- Add packages here -->

</struts>

5、新建jsp文件:index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello world
</body>
</html>

6、創建Action類 HelloAction.java

public class HelloAction {
	public String execute(){
		
		System.out.println("hello world");
		return null;
	}

}


運行:無需開啓tomcat,直接將tomcat部署到eclipse,選擇run as:run on server
運行結果爲






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章