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>