Step 1.Create jsp file
index.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<style type="text/css">
.errorMessage{color:red;}
</style>
</head>
<body>
<s:form action="reg">
<s:textfield name="uname" label="Username"></s:textfield>
<s:password name="upass" label="Password"></s:password>
<s:submit value="register"></s:submit>
</s:form>
</body>
</html>
Step 2.Create POJO Class
register.java
package com.java;
import com.opensymphony.xwork2.*;
public class register extends ActionSupport{
private static final long serialVersionUID = 1L;
private String uname,upass;
//Getter and Setter here
public String execute()
{return SUCCESS;}}
Step 3.Create Validation file(validation file name must be same as action class name)
register-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="uname">
<field-validator type="requiredstring">
<message>Name can't be blank</message>
</field-validator>
</field>
<field name="upass">
<field-validator type="requiredstring">
<message>Password can't be blank</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">9</param>
<message>
password must be 5 to 9
</message>
</field-validator>
</field>
</validators>
Step 4.Create struts.xml and web.xml
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="reg" class="com.java.register">
<result name="input">index.jsp</result>
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StrutsValidation</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
Welcome,<s:property value="username"/>
Step 5 : Add Required Jar Files
nice
ReplyDelete