struts入门小案例(了解struts基础配置)

一、导入jar包:

1.好像有种快速找到所需要jar包的方法:在下载的struts all的文件下,有一个app目录下,这个目录存放的是一些struts实例案例。我们可以利用压缩软件打开其中一个案例,找到该案例中的lib文件,复制它所导入的所有jar包。然而本人按照这个方法,里面的案例有很多jar包,似乎与我的需求有点不符。可能是因为我下载的struts版本太高的原因。(不能将struts中的lib目录下的所有jar包导入到项目里,这样也会报错)
2.以下是写一个入门案例所需的基本jar包:
在这里插入图片描述

二、Action类的编写

1.这里的action类可以理解为web中servlet。
2.代码:

package com.fxy.sturts.action;

public class HelloAction {
	
	public String execute()throws Exception{
		
		return "ok";	
	}
}

其中execute为默认方法。需要有一个返回值,这里返回了一个“ok”;

三、struts最核心配置文件,struts.xml。

1.这里的配置文件名称和存放位置都是固定不变的,名称为struts.xml,位置为src目录下面。
在这里插入图片描述
2.下面是配置文件的详细内容:
a.我们需要给改配置文件添加一个命名空间:

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

b. package标签:
name属性用来标识一个package标签,这个name可以自定义,但是不同的package标签的name属性不可以一样;
extends属性的值为struts-default,为固定值。使在package中定义的类具有action的属性。
namespace属性的默认值为“/”,这个和下面的action中name属性构成访问路径。
如下面的hellodemo,则访问路径为http://主机路径/hellodemo
c.action标签:
name属性为访问路径的最后一个,访问路径为http://主机路径/action中name名称(.action),后面的.action可以不带,不带可能在一些小众的浏览器中会访问不到,大部分的浏览器带不带.action都没有影响。
class属性为我们写的Action类的全路径。
method属性表示我们需要执行的方法名称,在后面我们会仔细讨论method,这里先不赘述。
d.result标签表示所执行的方法的返回值。
name属性为返回值的确定值,这里的"ok"就是execute方法返回值,这里的“ok”与方法的返回值匹配上后,就会跳转到后面所写的页面。后面接着的为跳转的页面,hello.jsp文件要放在webapp目录下面,不然服务器访问不到该页面。我在这里消耗了好多时间。
type属性:表示页面跳转的方式。这里有两种,重定向和转发。其中转发不会改变访问路径。重定向会改变访问路径。

<?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>
<!-- 
	1.package name可以任意值,但是不同的package name不能相同
	2.extends的值为固定的struts-default,作用是在package标签里配置的类具有action属性。
	3.namespace:表示命名空间,与action中的name构成访问的最后路径(/hellodemo)。默认值为"/";
 -->
	<package name="hello" extends="struts-default" namespace="/">
	<!-- name配置访问路径的名称,class为action类的全路径名称 -->
		<action name="hellodemo" class="com.fxy.sturts.action.HelloAction" method="execute">
		<!-- result配置为action类的返回值
			 result还有一个type属性,表示的是页面跳转的方式(重定向或者转发)
		 -->
			<result name="ok">/hello.jsp</result>
		</action>
	</package>
</struts>
四、jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello struts ....</h1>
</body>
</html>

五、到这里我们还没有配置成功,不要忘记了配置过滤器。

过滤器我们在web.xml文件中配置。
这里有 < display-name >sturts2_case< /display-name>标签表示项目名称。
过滤器配置代码为:< filter-class>标签为固定值。这个类为StrutsPrepareAndExecuteFilter。

    <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>
    
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>sturts2_case</display-name>
    
    <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>
    
  <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>

到这里,我们可以将项目部署到服务器中,开启服务器,在浏览器中输入访问路径即可得到所想要的页面。
我这里输入http://localhost:8080/sturst2_case/hellodem。
在这里插入图片描述

一、编写Action类的三种方式:

1.编写普通的类,不继承和实现任何类。

就像上面写的案例一样。

2.实现Action类

在这里插入图片描述
3.继承ActionSupport方法:
在这里插入图片描述
这里做几点说明,Action类中的方法可以没有返回值,但是如果有返回值,就只能是String类型。
如果我们使用后两种方法来编写Action类,jdk给了我们一些常量:我们通过查看源代码可以看到,这些常量我们可以直接调用。根据下面源代码中的注释也可以很清楚的了解到每个常量相应使用的情景。
如果我们不需要使用返回值,那么在所执行的方法中放回常量NONE。struts.xml文件中的result标签也不需要配置。这种情况不能发生页面的跳转。

package com.fxy.sturts.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
	
	public String execute() {
		return "none";
	}
}

在这里插入图片描述
在这里插入图片描述

一、Action类中方法的三种访问方式:

Action默认方法为execute(),如果我们希望自定义方法,该怎么做呢,struts提供了三种方式,我们需要掌握前两种,最后一种动态获取不需要掌握。在这里我也就记下两种。
1.通过在action标签中配置method属性。method属性中填写要执行的方法名称。
我们可以通过添加多个action标签设置多个方法执行。但是每个action标签的name不可以一样。但是这个方法有缺陷。如果我们需要执行很多方法,我们就需要写很多action标签。这样太累赘。

<action name="hellodemo" class="com.fxy.sturts.action.HelloAction" method="execute">
<action name="othername" class="com.fxy.sturts.action.HelloAction" method="addbook">

2.通配符的方法:我们可以将action标签下的name属性设置为“某某_”,然后再method中填写“{1}”,这个代表第一个号。(name属性可以写成这样,某某__…,我们一般只用一个*号就可以了)。

<action name="hellodemo_*" class="com.fxy.sturts.action.HelloAction" method="{1}">

然后在访问路径中指定方法名称,如http://localhost:8080/sturts2_case/hellodemo_addbook;这就代表指定addbook方法。解决了上面的问题。

一、分模块开发:

当一个项目需要多个人同时开始时,为了保证代码的整体一致性,我们常用分模块开发。每个开发者写自己的配置文件,然后将所有的配置文件通过include标签引入到struts.xml文件中。
这个是struts.xml文件:

<?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>
<!-- 
	1.package name可以任意值,但是不同的package name不能相同
	2.extends的值为固定的struts-default,作用是在package标签里配置的类具有action属性。
	3.namespace:表示命名空间,与action中的name构成访问的最后路径(/hellodemo)。默认值为"/";
 -->
 
	<include file="com/fxy/sturts/action/hello.xml"></include>
</struts>

这个是hello.xml文件:

<?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>
<!-- 
	1.package name可以任意值,但是不同的package name不能相同
	2.extends的值为固定的struts-default,作用是在package标签里配置的类具有action属性。
	3.namespace:表示命名空间,与action中的name构成访问的最后路径(/hellodemo)。默认值为"/";
 -->
	<package name="hello" extends="struts-default" namespace="/">
	<!-- name配置访问路径的名称,class为action类的全路径名称 -->
		<action name="hellodemo" class="com.fxy.sturts.action.HelloAction" method="execute">
		<!-- result配置为action类的返回值
			 result还有一个type属性,表示的是页面跳转的方式(重定向或者转发)
		 -->
			<result name="ok">/hello.jsp</result>
		</action>
	</package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值