Struts2学习笔记(一)

一、入门

1.导入jar包(地址如下)

链接:https://pan.baidu.com/s/1QdY2mN1epu_l1x92uD6llQ
提取码:buno
jar包的导入,只需要导入需要的jar包就可以,如果不知道需要什么jar包,可以到apps目录下找到示例程序的war包,解压,然后粘贴到webContent/WEB-INF/lib中

2.创建Action
(1)Action是一个普通的JAVA类

在src目录下创建一个JAVA类,命名为Action(不固定),实现execute方法,代码如下:

package struts2_test;
public class Action {
	public String execute() {
		return "hello";
	}
}

在webContent目录下创建一个页面为hello.html(示例),代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2</title>
</head>
<body>
	Hello,Struts2!!!
</body>
</html>
(2)访问Action

访问Action默认执行Action类中的execute方法,因此Action需要实现execute方法。

(3)配置Action的访问路径
  • 创建struts2核心配置文件struts.xml,创建在src文件夹下
  • 引入dtd约束,将下方语句复制粘贴到struts.xml文件中
	<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

-添加 struts标签对 及其配置,xml结构如下

<?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="demo" extends="struts-default" namespace="/">
		<action name="hello" class="struts2_test.Action">
			<result name="hello">/hello.html</result>
		</action>
	</package>
</struts>
(4)配置struts2过滤器

过滤器是在WEB-INF目录下的web.xml文件中进行配置,代码如下

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>struts2_test</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>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
(5)访问

配置完成后,运行程序,访问路径为
http://127.0.0.1/项目名/Action标签name属性值.action
可以省略.action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值