eclipse学习(第二章:初识ssh)——19.struts的标签(②数据标签)
前言
本文参考自以下网站,这是自己实践后做的一些笔记
https://www.w3cschool.cn/struts_2/struts_data_tags.html
本文主要针对的是action标签(获取跳转内容标签)、include标签(嵌套标签)、bean标签(注入类标签)、date标签(设置日期格式标签)、参数标签(param标签)、获取值(property标签)、获取值(push标签)、获取一些文本(text标签)、url(url标签)的一些demo。
一开始的初始化项目和修改web.xml是必须的,然后看你要使用哪个标签就跳转过去加入对应的内容即可启动及测试成功了。
初始化项目及jar包拉取
修改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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssh_learn_data_label</display-name>
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
1、action标签的使用
创建ActionTest
这里主要是设置了一个值listA,到时候会在前端引用过去
package czx.label.action.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Servlet;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Namespace(value = "/user")
public class ActionTest extends ActionSupport{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
@Action(value="hi",
results = {
@Result(name="success",location = "/reference.jsp"),
@Result(name="error",location = "/error.jsp")
})
public String execute() throws Exception {
List<String> listA = new ArrayList<>();
listA.add("民");
listA.add("神");
listA.add("狼");
ServletActionContext.getRequest().setAttribute("listA", listA);
return "success";
}
}
创建相关的jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:action name="hi" executeResult="true" namespace="/user">
输出看看s:action的内容,executeResult属性要设成true,才能够引用到对应内容,默认是false<br/>
</s:action>
<br/><br/>
<s:iterator value="#attr.listA">
<s:property/><br/>
</s:iterator>
</body>
</html>
reference.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成功登陆访问的内容</title>
</head>
<body>
<s:generator separator="," val="%{'red,blue,yellow,green'}">
<s:iterator>
<s:property/><br/>
</s:iterator>
</s:generator>
这个页面就是用来被引用的
</body>
</html>
测试action标签
访问http://localhost:8080/ssh_learn_data_label/
2、include标签的使用
创建相关的jsp
include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3 style="color:red">include的使用</h3>
<s:include value="reference.jsp"></s:include>
</body>
</html>
如果你之前测试过action标签,那么就会有reference.jsp。如果没有的话,那么现在创建一个吧
reference.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成功登陆访问的内容</title>
</head>
<body>
<s:generator separator="," val="%{'red,blue,yellow,green'}">
<s:iterator>
<s:property/><br/>
</s:iterator>
</s:generator>
这个页面就是用来被引用的
</body>
</html>
测试include标签
访问http://localhost:8080/ssh_learn_data_label/include.jsp
3、使用bean标签
创建HiBean,注意这里必须要有无参构造器,不然会有问题
package com.label.bean.action;
public class HiBean {
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
private String name;
private String sex;
private Double height;
public HiBean() {
}
public HiBean(String name, String sex, Double height) {
super();
this.name = name;
this.sex = sex;
this.height = height;
}
public String hiTest() {
return "这个是hiTest()方法";
}
public String hiDemo(String name) {
return "看看名称:"+name;
}
}
创建相关的jsp
bean.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>s:bean的使用</title>
</head>
<body>
s:bean这里的是用来引用方法获取到其返回内容的,name指定位置,var/id指定后面引用名,里面设置的参数类似传参。<br/>
s:param里面的内容说明,name(字符串)-参数的名称,value(对象)-参数的值
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="first" value="6"></s:param>
<s:param name="last" value="10"></s:param>
</s:bean>
<br/>
<s:iterator value="counter">
<s:property /><br/>
</s:iterator>
<s:bean name="com.label.bean.action.HiBean" var="hiBean">
<s:param name="name" value="chen"></s:param>
<s:param name="sex" value="man"/>
<s:param name="height" value="180"/>
</s:bean>
<br/>
名称:<s:property value="#hiBean.name"/><br/>
方法A:<s:property value="#hiBean.hiTest()"/><br/>
方法B:<s:property value="#hiBean.hiDemo('老王')"/>
</body>
</html>
测试bean标签
访问http://localhost:8080/ssh_learn_data_label/bean.jsp
4、使用date标签
创建DateAction
这里主要是用来生成一个当前日期并存放起来
package com.label.date.action;
import java.util.Date;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
public class DateAction extends ActionSupport{
public Date getDatetime() {
return datetime;
}
public void setDatetime(Date datetime) {
this.datetime = datetime;
}
private Date datetime;
@Action(value = "dateTest",
results = {
@Result(name = "success",location = "/date.jsp"),
@Result(name="error",location = "/error.jsp")
})
public String dateHi() {
datetime = new Date();
return "success";
}
}
创建相关jsp
date.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>日期格式:日-月-年 时:分:秒</h3>
<s:date name="datetime" format="dd-MM-yyyy hh:mm:ss"/>
<h3>日期格式:年-月-日 时:分:秒</h3>
<s:date name="datetime" format="yyyy-MM-dd hh:mm:ss"/>
</body>
</html>
测试date标签
访问http://localhost:8080/ssh_learn_data_label/dateTest
5、使用param标签
这个比较简单,就是加参数,设置值。这些param标签可用于参数化其他标签。此标签具有以下两个参数。
name(字符串) - 参数的名称
value(对象) - 参数的值
这里的示例可以去参考bean标签。
6、使用property,push和set标签
创建User
package com.label.push.action;
public class User {
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User() {
super();
}
}
创建PushAction
PushAction
package com.label.push.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class PushAction extends ActionSupport{
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTestName() {
return testName;
}
public void setTestName(String testName) {
this.testName = testName;
}
private User user = new User("陈先生","12345678");
private String testName = "测试用名称";
@Action(value="properties",
results = {
@Result(name = "error",location = "/error.jsp"),
@Result(name="success",location = "/push.jsp")
})
public String test() {
return "success";
}
}
创建push.jsp
push.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>多方式获取属性值</h3>
<h4>第一种使用s:property</h4>
<s:property value="user.username"/>
<h4>第二种使用s:push</h4>
<s:push value="user">
<s:property value="username"/>
</s:push>
<h4>第三种使用s:set,这个不一样不能在s:set里面写s:property,要拎出来才对</h4>
<s:set name="newUser" value="user.username"></s:set>
<s:property value="newUser"/>
<br/>
三种方式比较:
第一种s:property方法,直接使用s:property获取环境名称的值,环境变量直接在对应的xx类中,直接user.username来引用即可。<br/>
属性少的时候使用第一种很好,但是如果属性多了,那么就麻烦了,要一个个加上“user.”。<br/>
第二种s:push直接把user抽取上去,然后在获取一个个里面的属性就少写了很多了。<br/>
第三种s:set是直接给对应的属性起个别名,这样如果我们需要多次调用该属性就不需要写太多了。<br/>
</body>
</html>
测试property,push和set标签
访问http://localhost:8080/ssh_learn_data_label/properties
7、text标签的使用
创建TextAction.properties
hi.testA=the first test data
hi.uu.kk=happy time
hi.ppp=play the games param: {0}
创建TextAction
package com.label.text.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class TextAction extends ActionSupport {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
@Action(value = "textLearn",
results = {
@Result(name = "error",location = "/error.jsp"),
@Result(name="success",location = "/text.jsp")
})
public String text() {
return "success";
}
}
创建text.jsp
text.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:i18n name="TextAction">
<s:text name="hi.testA"></s:text><br/>
<s:text name="hi.uu.kk"></s:text><br/>
不带参数:<s:text name="hi.ppp"></s:text><br/>
带参数:
<s:text name="hi.ppp">
<s:param> test</s:param>
</s:text><br/>
<s:text name="hi.sdsadaa">如果不存在的获取会怎样呢</s:text><br/>
<s:text name="hi.ddddddd"></s:text>
</s:i18n>
</body>
</html>
测试text标签
访问http://localhost:8080/ssh_learn_data_label/textLearn
8、url标签的使用
创建url.jsp
url.jsp
这里我们先定义一个url地址,然后在显示一个url地址
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:url id="myUrl" action="myUrl" var="myUrlTest">
<s:param name="username">li</s:param>
</s:url>
<a href="<s:property value='myUrlTest'/>">
<s:property value="myUrlTest"/>
</a>
</body>
</html>
测试url标签
访问http://localhost:8080/ssh_learn_data_label/url.jsp
点击这个链接,url地址变为
项目地址
这个仓库里面的ssh_learn_data_label
https://gitee.com/mrchen13427566118/ssh_learn.git
如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221