Olá, estou usando FrontController E tenho uma dúvida. Minhas URL’S são acessadas assim, EXEMPLO: http://localhost:8080/locadora/action/qualquer_coisa
[code]
FrontController
pacote.FrontController
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
[/code]
então o froncontroller faz tudo certinho… …
mas meu grande problema é que eu estou tendo que copiar a parte visual pra todos os .jsp’s… e isso não é bom.
tem como usando frontcontroller as url’s serem acessadas assim
http://localhost:8080/locadora/index.jsp?categoria=legal
segue a classE:
package pacote;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class FrontController extends HttpServlet {
private Map<String, String> commands = new Hashtable<String, String>();
private Map<String, String> forwards = new Hashtable<String, String>();
public void init() throws ServletException {
commands.put("cadastrar", "pacote.CadastraCommand");
commands.put("cadastrarUsuario", "pacote.CadastraUsuarioCommand");
commands.put("cadastraGenero", "pacote.CadastraGeneroCommand");
commands.put("cadastraCategoria", "pacote.CadastraCategoriaCommand");
commands.put("cadastraFilme", "pacote.CadastraFilmeCommand");
commands.put("logoutUser", "pacote.LogoutCommand");
commands.put("gerencia", "pacote.GerenciaCategoriaCommand");
commands.put("alteraCategoria", "pacote.AlteraCategoriaCommand");
commands.put("acompanharLocacoes", "pacote.AcompanharLocacoesCommand");
commands.put("detalhesLocacoes", "pacote.DetalhesLocacoesCommand");
forwards.put("detalhes", "/detalhes.jsp");
forwards.put("acompanhar", "/acompanhar.jsp");
forwards.put("gerenciaCategoria", "/gerenciaCat.jsp");
forwards.put("cadastra","/cadastrar.jsp");
forwards.put("logout", "/");
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
handleRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
handleRequest(request, response);
}
protected void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
int index = requestURI.lastIndexOf("/");
String requestCommand = requestURI.substring(index + 1);
String commandClass = (String) commands.get(requestCommand);
if (commandClass == null) {
throw new ServletException(
"Não foi possível encontrar o comando para a requisição:"
+ requestCommand);
}
try {
AbstractCommand cmd = (AbstractCommand) Class.forName(commandClass)
.newInstance();
cmd.init(request, response);
String target = cmd.execute();
String jsp = (String) forwards.get(target);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jsp);
dispatcher.forward(request, response);
} catch (ClassNotFoundException ex) {
throw new ServletException(
"Não foi possível encontrar a classe de comando:"
+ commandClass);
} catch (IllegalAccessException ex) {
throw new ServletException(
"Não foi possível instanciar a classe de comando:"
+ commandClass);
} catch (InstantiationException ex) {
throw new ServletException(
"Não foi possível instanciar a classe de comando:"
+ commandClass);
}
}
}