Tservarius
Mitglied
Hi Leute,
allsooo habe da ein J2EE Projekt mit Struts und Tiles.
Ganz klassich werden Formulareingeaben zunächst durchs FormBean und danach durch die FormAction gejagd und entweder FormBean oder FormAction schmeisst hallt bei Fehlern, error Raus, welche denn auf der <forward failure> page angezeigt werden.
Soweit so gut funktioniert auch alles, AABER: ich benutze auch noch das TILES Framework.
Wird nun eine Error Seite angezeigt, passiert dies ohne jegliche Einbettung in das Layout,
es resultiert wirklich nur die Seite ohne Header / Footer, die das Layout ausmachen.
Jemand ne Ahnung was ich falsch gemacht habe?
Hier mal meine TILES-Def.xml:
und hier die struts-config:
hier ein beispiel (login):
login-jsp:
login-formbean:
login:action:
allsooo habe da ein J2EE Projekt mit Struts und Tiles.
Ganz klassich werden Formulareingeaben zunächst durchs FormBean und danach durch die FormAction gejagd und entweder FormBean oder FormAction schmeisst hallt bei Fehlern, error Raus, welche denn auf der <forward failure> page angezeigt werden.
Soweit so gut funktioniert auch alles, AABER: ich benutze auch noch das TILES Framework.
Wird nun eine Error Seite angezeigt, passiert dies ohne jegliche Einbettung in das Layout,
es resultiert wirklich nur die Seite ohne Header / Footer, die das Layout ausmachen.
Jemand ne Ahnung was ich falsch gemacht habe?
Hier mal meine TILES-Def.xml:
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<!-- Definition der globalen Einstellungen -->
<definition name="base.definition" path="/jsp/layout.jsp">
<put name="title" value="Test" />
<put name="logo" value="images/logo.gif" />
<put name="favicon" value="favicon.ico" />
</definition>
<!-- Login Seiten Definition -->
<definition name="page.login" extends="base.definition" path="/jsp/layout.jsp">
<put name="body" value="/jsp/index.jsp" />
</definition>
<!-- Monitor Definition -->
<definition name="page.monitor" extends="base.definition">
<put name="body" value="/jsp/monitor.jsp" />
</definition>
</tiles-definitions>
und hier die struts-config:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="LoginForm" type="xyz.webtransportmonitor.forms.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards >
<forward name="showWelcome" path="/showWelcome.do" redirect="true" />
<forward name="showMonitor" path="/showMonitor.do" redirect="true" />
</global-forwards>
<action-mappings>
<action forward="page.login" path="/showWelcome" />
<action
input="/jsp/index.jsp"
name="LoginForm"
path="/doLogin"
scope="request"
type="xyz.webtransportmonitor.actions.LoginAction">
<forward name="failure" path="page.login" />
<forward name="success" path="page.monitor" />
</action>
</action-mappings>
<message-resources parameter="xyz.webtransportmonitor.ApplicationResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-parser-validate" value="true" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
</plug-in>
</struts-config>
hier ein beispiel (login):
login-jsp:
Code:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles"
prefix="tiles"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template"
prefix="template"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
prefix="nested"%>
<div class="introtext">
<h1>
Anmeldung
</h1>
Bitte loggen Sie sich mit Ihren Benutzerdaten ein
<html:form action="/doLogin">
<table class=normal>
<tr>
<td>
Username
</td>
<td>
<html:text property="uname" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td>
Passwort
</td>
<td>
<html:password property="upwd" size="30" maxlength="30"/>
</td>
</tr>
</table>
<font color="red"><html:errors/></font>
<p/>
<html:submit>Einloggen</html:submit>
</html:form>
</div>
login-formbean:
Code:
package xyz.webtransportmonitor.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm {
private static final long serialVersionUID = 6573055891392624314L;
// Login-Parameters from Form
private String uname = null; //submitted username
private String upwd = null; //submitted password
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
//username present?
if ( getUname()==null || getUname().length() < 1) {
errors.add("uname",new ActionError("error.username.required"));
}
//userpassword present?
if (getUpwd()==null || getUpwd().length() < 1) {
errors.add("upwd",new ActionError("error.password.required"));
}
//Return of possible error-messages in error object
return errors;
}
/*
* Getters and Setters
*/
public String getUname() {
return uname;
}
public String getUpwd() {
return upwd;
}
public void setUname(String uname) {
this.uname = uname;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
login:action:
Code:
package xyz.webtransportmonitor.actions;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.ListIterator;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xyz.webtransportmonitor.Crypt;
import xyz.webtransportmonitor.FileAccess;
import xyz.webtransportmonitor.Login;
import xyz.webtransportmonitor.forms.LoginForm;
public class LoginAction extends Action
{
Login loginListe;
FileAccess datei = new FileAccess();
Crypt crypt = new Crypt();
// set up Datetime Format
DateFormat date_german_long = DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.MEDIUM, Locale.GERMANY);
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
//hier den check, ob der user gültig ist
boolean loginStatus=false;
//get entered values
LoginForm objForm = (LoginForm) form;
String user_username =objForm.getUname();
String user_password =objForm.getUpwd();
//Check if Username is correct
if (checkLogin(user_username, user_password) == true) {
// User ist vorhanden und wurde authentifiziert
loginStatus=true;
} else {
// Userpasswort hat nicht gestimmt
loginStatus=false;
}
//check if login was successfull
if (loginStatus==true) {
//Get current session and paste parameters from form
//Vorname, nachname, username, passwort, email in session packen
HttpSession session = request.getSession();
ArrayList daten = datei.readData();
ListIterator li = daten.listIterator();
while (li.hasNext()) {
this.loginListe = (Login) li.next();
String vorname = this.loginListe.getVorname();
String nachname = this.loginListe.getNachname();
String email = this.loginListe.getEmail();
String passwort = this.loginListe.getPasswort();
String loginzeit = this.loginListe.getLetzterLogin();
String tmpUsername = vorname + " " + nachname;
if (user_username.equalsIgnoreCase(tmpUsername)) {
//User found, set up session vars
session.setAttribute("vorname", vorname);
session.setAttribute("nachname", nachname);
session.setAttribute("username", tmpUsername);
session.setAttribute("email", email);
session.setAttribute("passwort", passwort);
session.setAttribute("loginzeit", loginzeit);
//set up empty selection vars
session.setAttribute("parameter_belegnummer", "");
session.setAttribute("parameter_debitornummer", "");
session.setAttribute("parameter_wenummer", "");
}
}
//procceed login
doLogin(user_username);
//Everything went fine - return success page
return mapping.findForward("success");
} else {
//Creats error-object
ActionErrors errors = new ActionErrors();
errors.add("login",new ActionError("error.nomatchpass"));
saveErrors(request,errors);
//Errors occured - return error page / login page
return mapping.findForward("failure");
}
}
/**
* Helping Methods for validation and registration of users
*/
public boolean checkLogin(String UserName, String Passwort) {
//checks if user is present in loginfile
boolean auth = false;
String Passworts = crypt.encrypt(Passwort);
//Check if login is in arraylist which includes file-data
ArrayList daten = datei.readData();
ListIterator li = daten.listIterator();
while (li.hasNext()) {
this.loginListe = (Login) li.next();
String vorname = this.loginListe.getVorname();
String nachname = this.loginListe.getNachname();
String passwort = this.loginListe.getPasswort();
String tmpUsername = vorname + " " + nachname;
if (UserName.equalsIgnoreCase(tmpUsername)) {
// Username is present in arraylist / file
if (Passworts.equals(passwort)) {
// Username and Password match
auth = true;
return auth;
} else {
// Username and Password doesn't match
auth = false;
return auth;
}
}
}
return auth;
}
public void doLogin(String Username) {
// get current datetime
Date tmpDate = new Date(); // Aktueller Timestamp
String tmpDateString = date_german_long.format(tmpDate);
String letzerLogin = tmpDateString;
ArrayList daten = datei.readData();
ListIterator li = daten.listIterator();
while (li.hasNext()) {
this.loginListe = (Login) li.next();
String vorname = this.loginListe.getVorname();
String nachname = this.loginListe.getNachname();
String tmpUsername = vorname + " " + nachname;
// set current datetime into right user
if (tmpUsername.equals(Username)) {
this.loginListe.setLetzterLogin(letzerLogin);
}
}
// write arraylist data
datei.writeData(daten);
}
}