Struts2的配置與簡單案例:
1. 創(chuàng)建一個(gè)dynamic web project(創(chuàng)建時(shí)讓它自動(dòng)生成web.xml文件)
2.引入相關(guān)jar包
3.在web.xml中進(jìn)行配置
(啟動(dòng)tomcat服務(wù)器之后第一個(gè)加載的文件就是web.xml)
在配置中添加過濾器:
1
2
3
4
5
6
7
8
|
<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> |
4.創(chuàng)建struts的核心文件(struts.xml),將其創(chuàng)建在Java Resources-src文件目錄下,內(nèi)容為:
1
2
3
4
5
6
|
<? 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 > </ struts > |
5.同樣在Java Resources-src文件目錄下創(chuàng)建一個(gè)Action類,繼承自ActionSupport,并覆蓋父類中的execute方法:
1
2
3
4
5
6
7
|
public class HelloWorldAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println( "執(zhí)行Action" ); return SUCCESS; } } |
6.在struts.xml中的<struts>標(biāo)簽中添加:
1
2
3
4
5
6
7
|
< struts > < package name = "default" namespace = "/" extends = "struts-default" > < action name = "helloworld" class = "default package.HelloWorldAction" > < result >/result.jsp</ result > </ action > </ package > </ struts > |
7.創(chuàng)建視圖(在WebRoot目錄下創(chuàng)建result.jsp):
1
2
3
|
< body > This is result.jsp! </ body > |
8.調(diào)試運(yùn)行
希望本篇文章對您有所幫助