Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am do according to guide of apache struts tutorial.
In that lession, I do validate value from input field. In struts.xml have 2 line code similar but belong 2 action with 2 method difference. If I drop 1 in 2
<result name="input">/register.jsp</result>
, it will show problem.
Anybody explain to my why.

struts.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<constant name="struts.devMode" value="true" />

	<package name="basicstruts2" extends="struts-default">
		<action name="hello"
			class="org.apache.struts.helloworld.action.HelloworldAction" method="execute">
			<result name="success">/HelloWorld.jsp</result>
		</action>
		
		<action name="index">
			<result>/index.jsp</result>
		</action>
		
		<action name="register" class="org.apache.struts.register.action.Register" method="execute">
			<result name="success">/thankyou.jsp</result>
			<result name="input">/register.jsp</result> 
		</action>
		
		<action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
		    <result name="input">/register.jsp</result>
		</action> 
	</package>

</struts>


Register.java
Java
package org.apache.struts.register.action;

import org.apache.struts.register.model.Person;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport{
	private Person personBean;
	
	@Override
	public String execute() throws Exception {
	
		return SUCCESS;
	}

	public Person getPersonBean() {
		return personBean;
	}

	public void setPersonBean(Person personBean) {
		this.personBean = personBean;
	}
	
	public void validate(){
		if ( personBean.getFirstName().length() == 0){
			addFieldError("personBean.firstName", "First name is required.");
		}
		
		if (personBean.getEmail().length() == 0){
			addFieldError("personBean.email", "Email is required.");
		}
		
		if (personBean.getAge() < 18){
			addFieldError("personBean.age", "Age is required and must be 18 or older");
		}
	}
}


register.jsp

ASP.NET
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
<s:head/>
</head>
<body>
<h3>Register for a prize by completing this form.</h3>

<s:form action="register">
	<s:textfield key="personBean.firstName"></s:textfield>
	<s:textfield key="personBean.lastName"></s:textfield>
	<s:textfield key="personBean.email"></s:textfield>
	<s:textfield key="personBean.age"></s:textfield>
	
	
	<s:submit></s:submit>
	
</s:form>

</body>
</html>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900