我开发了一个jsp页面,其中需要提供用户详细信息。这是jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="form_container" align="center">
<div class="errorMessages" >
<s:actionerror/>
</div>
<s:if test="hasActionMessages()" > <!-- Print action messages if any -->
<div class="successMessages" >
<s:actionmessage/>
</div>
</s:if>
<s:form action="registerOphthalmologist" method="post">
<s:hidden key="role" label="role" value="O" />
<s:textfield key="username" label="Choose UserName" required="true"/>
<s:password key="password" label="Choose Password" required="true"/>
<s:textfield key="givenName" label="Name" required="true"/>
<s:textfield key="surname" label="Surname" required="true"/>
<s:textfield key="age" label="Age" required="true" value=""/>
<s:select key="gender" label="Gender"
list="#{'M':'Male','F':'Female'}"
required="true"/>
<s:textfield key="contactNumber" name="contactNumber" label="Contact Number" required="true"/>
<s:textfield key="email" name="email" label="email" required="true"/>
<s:textfield key="city" name="city" label="City" required="true"/>
<s:textfield key="district" name="district" label="District" required="true"/>
<s:textfield key="state" name="state" label="State" required="true"/>
<s:textfield key="country" name="country" label="country" required="true"/>
<s:textfield key="postalCode" name="postalCode" label="Postal Code" required="true"/>
<s:textfield key="degree" label="Qualification" required="true"/>
<s:textfield key="university" label="University" required="true" value=""/>
<s:textfield key="hospitalName" label="Hospital Name" required="true"/>
<s:textfield key="experience" label="Years of Experience" required="true" value=""/>
<s:checkbox key="checkMe" fieldValue="true" label="Agree to terms & Conditions"/>
<s:submit value="Register" align="center"/>
</s:form>
</div>
<div class="footer">
<p align="center">Designed and maintained by SSSIHL</p>
</div>
这将转到以下操作文件。
package com.ActionClasses;
import com.Daos.RegisterDao;
import com.Daos.UsernameAvailabilty;
import com.Beans.RegisterBeans;
import com.mysql.jdbc.StringUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport implements Action,ModelDriven<RegisterBeans>
{
RegisterBeans registerBeans = new RegisterBeans();
public void validate(){
System.out.println("came here "+ registerBeans.getUsername());
if (StringUtils.isNullOrEmpty(registerBeans.getUsername()))
{
System.out.println("username not found "+ registerBeans.getUsername());
addFieldError("username","Username is required");
return;
}
/*boolean Available = UsernameAvailabilty.check(registerBeans.getUsername());
if(!Available){
System.out.println("username not available "+ registerBeans.getUsername());
addFieldError("username","Username is not available");
return;
}
*/
}
public String execute() throws Exception{
if(RegisterDao.save(registerBeans) == 4) /*For every registration 4 tables will be filled in database*/
return SUCCESS;
return ERROR;
}
@Override
public RegisterBeans getModel() {
return registerBeans;
}
}
RegisterBeans.java包含所有getter和setter。当我没有在jsp中提供任何用户名时,我能够看到消息&#34;来到这里&#34;在控制台。但是当控制权返回register.jsp时,屏幕上不会显示错误消息。我做错了什么
我的struts.xml代码是
<action name="register*" class="com.ActionClasses.RegisterAction">
<result name="success"> /login.jsp </result>
<result name="input"> /register{1}.jsp </result>
</action>
答案 0 :(得分:0)
如果您使用simple
主题,则不会自动呈现字段错误。
你需要自己做:
<s:textfield key="username" label="Choose UserName" required="true"/>
<s:fielderror fieldName="username" />
另请注意,如果您使用的是Struts2的最新版本,则需要=“true”=“required”。