Skip to content

Instantly share code, notes, and snippets.

@jrbalsas
Last active March 26, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrbalsas/9199f3396d08429c4f5f574ad4dd1c90 to your computer and use it in GitHub Desktop.
Save jrbalsas/9199f3396d08429c4f5f574ad4dd1c90 to your computer and use it in GitHub Desktop.
DAW P8 fragmentos de código
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
}
}
@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");
}
}
@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";
}
}
<%@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>
@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;
}
@jrbalsas
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment