15
Struts2 Marco Antonio Abril 2009

Struts2

  • Upload
    ziv

  • View
    49

  • Download
    1

Embed Size (px)

DESCRIPTION

Marco Antonio Abril 2009. Struts2. Configuração. Criar o Dynamic Web Project conforme a imagem. Baixar as bibliotecas do Struts2 de http://marcoreis.net/BibliotecasStruts2.zip . Criar o diretório lib e descompactar as bibliotecas. Logo em seguida, adicione-as no Build Path. - PowerPoint PPT Presentation

Citation preview

Page 1: Struts2

Struts2Marco AntonioAbril 2009

Page 2: Struts2

Configuração

• Criar o Dynamic Web Project conforme a imagem.• Baixar as bibliotecas do Struts2 de

http://marcoreis.net/BibliotecasStruts2.zip.• Criar o diretório lib e descompactar as bibliotecas.

Logo em seguida, adicione-as no Build Path.• Adicione todas as bibliotecas no servidor (Tomcat).• Crie os arquivos package.properties, struts.xml, struts-

modulo-gerente.xml no src do projeto.• Os demais arquivos serão criados por demanda.

Page 3: Struts2

web.xml<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>SistemaBancarioStruts2Web-</display-name>

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<init-param>

<param-name>actionPackages</param-name>

<param-value>com.sistemabancario.apresentacao</param-value>

</init-param>

</filter>

Page 4: Struts2

web.xml

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

Page 5: Struts2

index.html

<html>

<body onload="window.location='sb/TelaDeLogin.action'">

Carregando...

</body>

</html>

Page 6: Struts2

TelaPrincipal.jsp

<%@taglib prefix="s" uri="/struts-tags"%>

<%@ 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">

<html>

<head>

<link href="<s:url value='css/Estilo.css'/>" rel="stylesheet"

type="text/css" />

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>.:Sistema Bancario:.</title>

</head>

<body>

Page 7: Struts2

TelaPrincipal.jsp

<!-- Inicio da pagina -->

<table align="center">

<tr>

<td><s:url id="cadastro" action="TelaCadastroDeGerente" /> <s:a href="%{cadastro}">Cadastro de Gerente</s:a> ||</td>

<td><s:url id="cadastro" action="TelaConsultaGerentes" /> <s:a href="%{cadastro}">Consulta Gerente</s:a></td>

</tr>

</table>

<table align="center">

<tr>

<td>

<h1><s:text name="mensagem.sistema" /></h1>

</td>

</tr>

</table>

<!-- Fim da pagina -->

</body>

</html>

Page 8: Struts2

struts.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.enable.DynamicMethodInvocation" value="false" />

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

<constant name="struts.custom.i18n.resources" value="package" />

<include file="struts-modulo-gerente.xml" />

<package name="apresentacao" namespace="/sb" extends="struts-default">

<action name="TelaDeLogin">

<result>/Login.jsp</result>

</action>

</package>

</struts>

Page 9: Struts2

Login.jsp

<%@taglib prefix="s" uri="/struts-tags"%><%@ 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"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title><s:text name="login.titulo" /></title></head><body>

Page 10: Struts2

Login.jsp

<!-- Inicio da pagina --><table border="1" align="center"><tr><td><s:form><s:property value="getText('login.cabecalho')" />(getText)<br /><s:text name="login.cabecalho" /><br /><s:label name="mensagem" theme="simple" /><br /><s:textfield name="usuario.login" label="Login:" /><s:password name="usuario.senha" label="Senha:" /><s:submit action="BemVindo" value="Ok" /></s:form></td></tr></table><!-- Fim da pagina -->

</body></html>

Page 11: Struts2

package.properties

#

mensagem.sistema=Bem-Vindo ao Sistema Bancario

#

login.titulo=Login - Sistema Bancario

login.cabecalho=Informe login e senha (properties)

Page 12: Struts2

Login.java

package com.sistemabancario.apresentacao;

import com.sistemabancario.entidades.*;

public class LoginAction { private Usuario usuario; private String mensagem = "Informe login e senha (LoginAction)";

public Usuario getUsuario() { if (usuario == null) { usuario = new Usuario(); } return usuario; }

public String getMensagem() { return mensagem; }

public void setMensagem(String mensagem) { this.mensagem = mensagem; }

Page 13: Struts2

Login.java

public String efetuarLogin() { if (getUsuario().getSenha().equals("senha")) { return "acessoLiberado"; } else { setMensagem("Senha inv‡lida"); return "acessoNaoLiberado"; } }

}

Page 14: Struts2

struts-modulo-gerente.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>

<package name="gerente" namespace="/sb" extends="struts-default">

</package>

</struts>

Page 15: Struts2

Para rodar a aplicação

• O link para rodar a aplicação:– http://localhost:8080/SistemaBancarioStruts2Web