在導(dǎo)入jar包和配置web.xml后牌芋,進(jìn)行下面的操作
一乎完、創(chuàng)建Action類
package action;
/**
*類說明 :Struts2框架使用Action類處理用戶請(qǐng)求
*@author :大湛湛
*/
public class HelloAction {
/**
* Action類中的方法必須用public修飾
* 必須有返回值,且返回值類型為String
* 方法名稱沒有要求亡容,但是不能有參數(shù)列表
*/
public String hello(){
//訪問方法時(shí)嗤疯,在控制臺(tái)打印hello struts2
System.out.println("hello struts2");
return null;
}
}
二、配置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>
<!-- 包結(jié)構(gòu) -->
<package name="default" namespace="/" extends="struts-default">
<!-- 配置Action -->
<action name="hello" class="action.HelloAction" method="helloAction"/>
</package>
</struts>
三萍倡、發(fā)布到服務(wù)器身弊,在網(wǎng)頁地址欄拼接hello.action進(jìn)行訪問測(cè)試
地址欄輸入http://localhost:8080/strutsTest/hello.action
控制臺(tái)打印出hello struts2,說明訪問action成功