Struts2基礎-helloworld

Struts2開發步驟

1.新建web工程Strutsdemo並引入jar包:

commons-fileupload-1.2.1.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.jar

xwork-core-2.1.6.jar

2.在web.xml中添加控制器配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<filter>
	<filter-name>strutsdemo</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
	<filter-name>strutsdemo</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
3.創建action
    package demo;
    public class Test {
       
	private String name;
	public String execute(){
		name = "hello world";
		return "success";
	}
	public String getName() {
		return name;
	}
}

4.在src下創建struts.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name = "demo1" extends = "struts-default">
<action name = "welcome" class = "demo.Test">
<result name = "success">hello.jsp</result>
</action>
</package>
</struts>

5.創建hello.jsp文件

<html>
  <head>
    <title>demo</title>
  </head>
  <body>
   ${name } <br>
  </body>
</html>

6.在瀏覽器地址欄發送請求:

http://127.0.0.1:8888/Strutsdemo/welcome.action

瀏覽器顯示:

hello world


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