struts数据校验

实现:商品评论页面 ,校验通过跳转至评价成功页面,校验不通过则给出处理失败的原因showValidationError.jsp

演示:
在这里插入图片描述

代码:

Comment .java

package comment;

public class Comment {
	int grade;
	String commentStr;
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public String getCommentStr() {
		return commentStr;
	}
	public void setCommentStr(String commentStr) {
		this.commentStr = commentStr;
	}
	
}

CommentAction.java

package comment;
import com.opensymphony.xwork2.ActionSupport;

public class CommentAction extends ActionSupport{
	Comment userComm;
	public Comment getUserComm() {return userComm;}
	public void setUserComm(Comment userComm) {this.userComm = userComm;}
	public void validateCommented(){
		System.out.println("针对commented()方法的数据进行校验");
		if(userComm.commentStr.length()<10 ||userComm.commentStr.length()>200 ){
			addFieldError("commentStr", "内容字数必须大于等于10个字符且不超过200个字符。");			
		}
	}
	public void validate(){
		System.out.println("validate()");
	}
	public String commented(){
		System.out.println(userComm.grade+"-"+userComm.commentStr);
		return "success";
	} 
}

在这里插入图片描述

前端:

comment.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  <body>
  	<form action="/web/sys/commentmng/comment_commented">
  		评分:<select name="userComm.grade">
  			<option value="0">0</option>
  			<option value="1">1</option>
  			<option value="2">2</option>
  			<option value="3">3</option>
  			<option value="4">4</option>
  			<option value="5">5</option>
  		</select>
  		
  		<br>
  		评价: <textarea name="userComm.commentStr"></textarea><br>
  		<input type="submit" value="提交"/>
  	
  	</form>
  </body>
</html>

在这里插入图片描述
校验通过页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  <body>
  评论成功!
  <br>
  </body>
</html>

在这里插入图片描述
校验不通过:
用标签:<%@ taglib uri="/struts-tags" prefix=“s” %>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  <body>

  <s:fielderror></s:fielderror>
  <s:actionerror/>
  </body>
</html>

struts的xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="sys" extends="pub" namespace="/sys/commentmng">
	<action name="comment_*" class="comment.CommentAction" method="{1}">
	</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="pub"  extends="struts-default">
	<global-results>
		<result name="input">/showValidationError.jsp</result>
		 <result name="success">/ok.jsp</result>
		 <result name="error">/showValidationError.jsp</result>
	</global-results>
</package>
<include file="comment/comment-actions.xml"></include>
</struts>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值