首页 > 其他分享 >struts1 标签

struts1 标签

时间:2023-04-26 19:33:43浏览次数:38  
标签:errors return String 标签 void private struts1 public


JSP引入标签


前提必须把struts-taglib包及其依赖包加到WEB-INF/lib目录。

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>



新建form类:

package com.struts1.form;

import org.apache.struts.action.ActionForm;

public class IndexForm extends ActionForm {

	private static final long serialVersionUID = 1L;

	private String username;
	
	private String password;
	
	private String house;
	
	private String car;
	
	private int age;
	
	private String sex;
	
	private String education;
	
	private String field;

	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;
	}

	public String getHouse() {
		return house;
	}

	public void setHouse(String house) {
		this.house = house;
	}

	public String getCar() {
		return car;
	}

	public void setCar(String car) {
		this.car = car;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}
	
}



在struts-config.xml里配置:

<form-bean name="indexForm" type="com.struts1.form.IndexForm" />




html标签



例子


index.jsp:



<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="common.jsp" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.struts1.entity.Education" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<html:base/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	List<Education> educationList = new ArrayList<Education>();
	educationList.add(new Education("1", "大专"));
	educationList.add(new Education("2", "本科"));
	request.setAttribute("educationList", educationList);
%>
<html:form action="index.do" method="post">
	<html:text property="username" /><br>
	<html:password property="password" /><br>
	<html:checkbox property="house" /><br>
	<html:checkbox property="car" /><br>
	<html:select property="age">
		<html:option value="18"></html:option>
		<html:option value="19"></html:option>
	</html:select>
	<br>
	<html:radio property="sex" value="male" /><br>
	<html:radio property="sex" value="female" /><br>
	<html:select property="education">
		<html:options collection="educationList" property="id" labelProperty="level" />
	</html:select><br>
	<html:select property="education">
		<html:optionsCollection name="educationList" label="level" value="id"/>
	</html:select><br>
	<html:link href="index.jsp">index.jsp</html:link><br>
	<html:button property="btn" οnclick="alert(123);"></html:button><br>
	<html:reset /><br>
	<html:textarea property="field">
	</html:textarea><br>
	<html:submit />
</html:form>
</body>
</html:html>



Education类:


package com.struts1.entity;

public class Education {
	private String id;
	private String level;

	public Education(String id, String level) {
		this.id = id;
		this.level = level;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}
}





bean标签



index.jsp:


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="common.jsp" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<html:base/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Collection<String> nameList = new ArrayList<String>();
nameList.add("123");
nameList.add("456");
request.setAttribute("nameList", nameList);
%>
<bean:define id="name" value="yang" />
<bean:write name="name"/><br>
<bean:struts id="indexForm" formBean="indexForm" />
<bean:write name="indexForm" property="type"/><br>
<bean:size id="size" name="nameList" />
<bean:write name="size"/><br>
<bean:message key="welcome.heading"/>
</body>
</html:html>



jsp输出:


 


yang
com.struts1.form.IndexForm
2
Welcome!




 


如果使用bean:size或者bean:message,需要在struts-config里配置



<message-resources parameter="MessageResources" />



在src目录下增加文件MessageResources.properties:


# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the MessageResources.properties file with this message in the /WEB-INF/src folder.)





logic标签




<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="common.jsp" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<html:base/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> nameList = new ArrayList<String>();
nameList.add("123");
nameList.add("456");
request.setAttribute("nameList", nameList);
%>
<bean:define id="name" value="yang" />
<logic:empty name="name">
	is empty
</logic:empty>
<br>
<logic:notEmpty name="name">
	is not empty
</logic:notEmpty>
<br>
<logic:present name="nameList">
	<logic:notEmpty name="nameList">
		<logic:iterate id="eachName" name="nameList" indexId="index">
			<bean:write name="index"/> <bean:write name="eachName"/><br>
		</logic:iterate>
	</logic:notEmpty>
</logic:present>
<logic:match value="yang" name="name">
match
</logic:match>
<br>
<logic:match value="y" name="name">
match
</logic:match>
<br>
</body>
</html:html>




标签:errors,return,String,标签,void,private,struts1,public
From: https://blog.51cto.com/u_1002776/6228670

相关文章

  • struts1 重复提交
    Struts的Token机制能够很好的解决表单重复提交的问题,基本原理是:服务器端在处理到达的请求之前,会将请求中包含的令牌值与保存在当前用户会话中的令牌值进行比较,看是否匹配。在处理完该请求后,且在答复发送给客户端之前,将会产生一个新的令牌,该令牌除传给客户端以外,也会将用户会话中......
  • Django模板层 (变量分配 过滤器 标签 继承和导入 自定义过滤器、标签及inclusion_ta
    目录一、模板变量分配定义 在后端变量的值通过模板语法传到前端符号{{}}:主要与数据值相关{%%}:主要与逻辑相关模板语法注意点:1.针对需要加括号调用的名字django模板语法会自动加括号调用你只需要写名字就行2.模板语法的注释{##},前端浏览器是无法查看的,因为它要先......
  • mybatis where标签动态sql问题
    使用where标签注意事项:where标签只会去掉第一个多出来的and和or,使用where标签时要把and放到前面这种情况下生成的SQL更干净,更贴切,不会在任何情况下都有where1=1这样的条件。<selectid="search"resultType="com.example.springweb2.pojo.Member"> selectid,name,a......
  • video标签如何加快加载速度
    视频标签(<video>)加载速度的优化可以从以下几个方面入手:压缩视频文件大小:可以使用视频压缩工具来压缩视频文件大小。压缩后的视频文件大小更小,加载速度更快。使用适当的视频格式:不同的浏览器支持不同的视频格式。因此,可以使用流行的视频格式,如MP4、WebM和Ogg,以确保视频在各种......
  • HTML+CSS学习--HTML表单标签
    表单1:表单标签<form></form>属性:action='接口地址'method='get/post'name='表单名称'2:表单控件<input>属性:type='控件类型'name:属性标识表单域的名称;Value:属性定义表单域的默认值,其他属性根据type的不同而有所变化。maxlength:控制最多输入的字符数,Size:控制框的宽度(......
  • HTML+CSS学习--HTML表单标签
     关注我了解更多web技术知识,带你一路“狂飙”到底!上岸大厂不是梦!表单1:表单标签<form></form>属性:action='接口地址'method='get/post'name='表单名称'2:表单控件<input>属性:type='控件类型'name:属性标识表单域的名称;Value:属性定义表单域的默认值,其他属......
  • 在vue标签代码块中定义变量
     方式一:<template><h1>test</h1><template:set="first=list[0]">//定义变量<div>{{first.name}}</div>//使用变量</template>...</template><script>exportdefault{......
  • Mybatis中的<![CDATA[]]>标签在判断日期场景中的使用
    背景在使用mybatis时我们sql是写在xml映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义。如大于号>会被转义为>转义后的可读性不是很直观,如果想让其看起来更加直观可读性更强的话,则需要使用<![CDATA[]]>来圈起来不被转义的符号以此来解决这个问题。......
  • Eclipse默认标签TODO,XXX,FIXME和自定义标签[转]
    1TODO表示需要实现,但目前还未实现的功能2XXX勉强可以工作,但是需要改进的功能3FIXME代码是错误的,不能工作,需要修复4.自定义标签window-->preferences-->java-->compiler-->tags选择new,输入tag,选择priority,ok上述所有注释都会被eclipseta......
  • 条码打印软件怎样制作弧形标签
    在使用条码打印软件制作标签的时候,不同的客户对于标签上文字的排版要求是不一样的。常见的方形标签,对于文字的排版可能不是那么多要求。如果是圆形标签,可能需要文字呈扇形排版或者是弧形排版(如下图),那么这种需要怎么做呢?今天小编就给大家演示一下弧形文字的制作过程。打开条码打......