第一步
创建一个web工程
第二步
引入struct2所必须的jar包
第三步
创建一个jsp文件,附上a标签,并可以发送请求
<a href="${pageContext.request.contextPath}/hello.action">点击</a>
方法名固定:
public String execute(){ }
第四步
编写一个action类以接受请求并处理
第五步
配置Struts.xml配置文件,配置好对应接受请求的action和对应的处理类
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="account" extends="struts-default"> <action name="hello" class="com.ctbu.action.HelloAction"/> </package> </struts>
第六步
在web.xml中配置前端控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--struts2核心过滤器配置--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
第七步
运行服务器并访问链接——>即可打印出方法内部的内容
拓展——配置一个结果页并跳转
在对应类的方法中返回一个字符串,并在struts2.xml配置文件中配置一个结果页
本文暂时没有评论,来添加一个吧(●'◡'●)