实现:商品评论页面 ,校验通过跳转至评价成功页面,校验不通过则给出处理失败的原因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>