Struts2框架——Struts的错误回显以及struts标签

Struts的错误回显以及struts标签

​ 本次记录一下 Struts 的错误回显以及 struts 框架前端标签的简单用法

Struts 的错误回显

​ 在 struts.xml 中的 result 标签中,把 name 属性的值设置成 input,这样当数据出现异常时,会自动回显到后边声明的 jsp 页面

1
2
3
4
<action name="register" class="com.lmh.action.UserAction" method="register">
<!-- 回显,出错后返回注册界面 -->
<result name="input">/register2.jsp</result>
</action>

struts 标签

​ struts 提供了可以在前端页面中使用的标签,使用 struts 标签,可以实现很多 struts 框架支持的高级功能

​ 首先,想要在前端页面中使用 struts 标签,需要在 jsp 页面中声明 struts 标签库

1
<%@taglib uri="/struts-tags" prefix="s" %>

​ 下面简单发一下 struts 标签的用法吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'register2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<!-- struts 表单的特点
1.action 不需要写项目名
2.会给表单里面的内容放在 table 中,并加上样式
3.struts 标签必须有 name 属性
-->
This is my JSP page. <br>
<s:form action="/login/register.action">
<s:textfield name="username" label="用户名"></s:textfield>
<s:textfield name="password" label="密码"></s:textfield>
<s:textfield name="birthday" label="生日"></s:textfield>
<!-- list 使用 OGNL 表达式 -->
<s:checkboxlist list="#{'coding':'写代码','basketball':'打篮球','soccer':'踢足球'}" label="爱好" name="hobby"></s:checkboxlist>
<s:radio list="#{'true':'已婚','false':'未婚'}" label="是否已婚" name="married"></s:radio>
<s:submit value="注册" name="zc"></s:submit>
</s:form>
<hr>
</body>
</html>
-------------本文结束感谢您的阅读-------------