Last active
March 26, 2019 08:49
-
-
Save jrbalsas/9199f3396d08429c4f5f574ad4dd1c90 to your computer and use it in GitHub Desktop.
DAW P8 fragmentos de código
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MuroWebApplicationInitializer | |
implements WebApplicationInitializer { | |
@Override | |
public void onStartup(ServletContext container) { | |
// Create the 'root' Spring application context | |
AnnotationConfigWebApplicationContext rootContext | |
= new AnnotationConfigWebApplicationContext(); | |
rootContext.register(SpringMvcConfig.class); | |
// Manage the lifecycle of the Spring root application context | |
container.addListener(new ContextLoaderListener(rootContext)); | |
// Create and map the Spring dispatcher servlet | |
ServletRegistration.Dynamic dispatcher | |
= container.addServlet("springMvcFrontDispatcher", | |
new DispatcherServlet(rootContext)); | |
dispatcher.setLoadOnStartup(1); | |
dispatcher.addMapping("/"); //SpringMVC route map | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebMvc | |
@ComponentScan(basePackages = {"com.daw.muro"}) | |
public class SpringMvcConfig implements WebMvcConfigurer { | |
@Override | |
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { | |
configurer.enable(); //Enable default controller, i.e. /* | |
} | |
@Bean(name = "viewResolver") | |
public InternalResourceViewResolver getViewResolver() { | |
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); | |
viewResolver.setPrefix("/WEB-INF/"); | |
viewResolver.setSuffix(".jsp"); | |
return viewResolver; | |
} | |
@Override | |
public void addViewControllers(ViewControllerRegistry registry) { | |
registry.addViewController("/").setViewName("redirect:index.html"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping("/muro") | |
public class MuroSpringController { | |
@Autowired | |
private Preferencias prefs; | |
@GetMapping("/identificador") | |
public String cambiarIdentificadorForm() { | |
return "muro/identificador"; | |
} | |
@PostMapping("/identificador") | |
public String cambiarIdentificadorForm( | |
@RequestParam(value="identificador",required=true) | |
String identificador, ModelMap model) { | |
prefs.setUsuario(identificador); | |
return "redirect:mensajes"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> | |
<!-- texto html omitido --> | |
<form:form method="POST" modelAttribute="mensaje"> | |
<p><form:errors path="*" cssStyle="color: red;"/></p> | |
<form:input path="texto" placeholder="Escribe tu mensaje"/> | |
<input type="submit" name="enviar" value="Enviar"> | |
</form:form> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping("/mensajes") | |
public String mensajesForm( | |
@ModelAttribute("mensaje") @Valid Mensaje m, | |
BindingResult result //Después del bean validado | |
) { | |
String view; | |
if (!result.hasErrors()) { | |
m.setIdentificador( prefs.getUsuario() ); | |
mensajeDAO.crea(m); | |
view="redirect:mensajes"; | |
} else { //show form with errors | |
model.addAttribute("mensajes", mensajeDAO.buscaTodos()); | |
view="muro/mensajes"; | |
} | |
return view; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fragmentos del guión 8 de prácticas para evitar los códigos adicionales que se pueden introducir al copiar/pegar desde un documento PDF