Rar! 0s
t 5 3Eq^E0 snippets\pom.xml ?
4.0.0
org.javamexico.snippets
snippets
1.0-SNAPSHOT
war
snippets
${project.build.directory}/endorsed
UTF-8
4.1.1.RELEASE
javax
javaee-web-api
7.0
provided
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.slf4j
slf4j-simple
1.7.7
javax.servlet
jstl
1.2
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.8
1.8
${endorsed.dir}
org.apache.maven.plugins
maven-war-plugin
2.5
false
org.apache.maven.plugins
maven-dependency-plugin
2.9
validate
copy
${endorsed.dir}
true
javax
javaee-endorsed-api
7.0
jar
Dt f J J =
Ol^E0A snippets\src\main\java\org\javamexico\snippet\domain\Snippet.java g4package org.javamexico.snippet.domain;
public class Snippet {
private int id;
private String title;
private String language;
private String description;
private String content;
public Snippet() {
}
public Snippet(int id, String title, String language, String description, String content) {
this.id = id;
this.title = title;
this.language = language;
this.description = description;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
xt { O|^E0V snippets\src\main\java\org\javamexico\snippet\service\config\ServiceConfiguration.java )package org.javamexico.snippet.service.config;
import org.javamexico.snippet.service.SnippetService;
import org.javamexico.snippet.service.impl.SnippetServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServiceConfiguration {
@Bean
public SnippetService getSnippetService() {
return new SnippetServiceImpl();
}
}
Gt w =ta_E0R snippets\src\main\java\org\javamexico\snippet\service\impl\SnippetServiceImpl.java package org.javamexico.snippet.service.impl;
import java.util.List;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.javamexico.snippet.domain.Snippet;
import org.javamexico.snippet.service.SnippetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SnippetServiceImpl implements SnippetService {
private static final Logger LOG = LoggerFactory.getLogger(SnippetServiceImpl.class);
private static final NavigableMap map = new TreeMap<>();
@Override
public void add(Snippet snippet) {
Objects.requireNonNull(snippet, "The snippet can not be null!");
int id = map.isEmpty() ? 1 : map.lastKey() + 1;
snippet.setId(id);
map.put(id, snippet);
if (LOG.isInfoEnabled()) {
LOG.info("Snippet added -- Id: {}", snippet.getId());
}
}
@Override
public void remove(int id) {
Snippet removed = map.remove(id);
if (LOG.isInfoEnabled()) {
LOG.info("Snippet removed -- Id: {}", removed.getId());
}
}
@Override
public void update(Snippet snippet) {
Objects.requireNonNull(snippet, "The snippet can not be null!");
Snippet updated = map.replace(snippet.getId(), snippet);
if (LOG.isInfoEnabled()) {
LOG.info("Snippet updated -- Id: {}", updated.getId());
}
}
@Override
public List findAll() {
return map.values().stream().collect(Collectors.toList());
}
@Override
public Snippet findById(int id) {
return map.get(id);
}
@Override
public List findByLanguage(String language) {
Objects.requireNonNull(language, "The language can not be null!");
return map.values().stream().filter(s -> language.equals(s.getLanguage())).collect(Collectors.toList());
}
}
t n \Ra_E0I snippets\src\main\java\org\javamexico\snippet\service\SnippetService.java \ԓpackage org.javamexico.snippet.service;
import java.util.List;
import org.javamexico.snippet.domain.Snippet;
public interface SnippetService {
public void add(Snippet snippet);
public void remove(int id);
public void update(Snippet snippet);
public List findAll();
public Snippet findById(int id);
public List findByLanguage(String language);
}
:Yt s K6]_E0N snippets\src\main\java\org\javamexico\snippet\web\config\WebConfiguration.java package org.javamexico.snippet.web.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan("org.javamexico.snippet.web")
public class WebConfiguration extends WebMvcConfigurationSupport {
@Override
protected void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
registry.viewResolver(viewResolver);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
}
Ht o 5JkF`{_E0J snippets\src\main\java\org\javamexico\snippet\web\MyWebAppInitializer.java zspackage org.javamexico.snippet.web;
import org.javamexico.snippet.service.config.ServiceConfiguration;
import org.javamexico.snippet.web.config.WebConfiguration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return new Class>[]{ServiceConfiguration.class};
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class>[]{WebConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
t m 2;v_E0H snippets\src\main\java\org\javamexico\snippet\web\SnippetController.java bpackage org.javamexico.snippet.web;
import org.javamexico.snippet.domain.Snippet;
import org.javamexico.snippet.service.SnippetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SnippetController {
@Autowired
private SnippetService snippetService;
@RequestMapping(value = "/all", method = RequestMethod.GET)
public String seeAll(Model model) {
model.addAttribute("list", snippetService.findAll());
return "all";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addSnippet(@RequestParam("title") String title, @RequestParam("language") String language, @RequestParam("description") String description, @RequestParam("content") String content, Model model) {
Snippet snippet = new Snippet(0, title, language, description, content);
snippetService.add(snippet);
return "redirect:all";
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String editSnippet(@RequestParam("id") int id, Model model) {
model.addAttribute("snippet", snippetService.findById(id));
return "edit";
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String addSnippet(@RequestParam("id") int id, @RequestParam("title") String title, @RequestParam("language") String language, @RequestParam("description") String description, @RequestParam("content") String content, Model model) {
Snippet snippet = new Snippet(id, title, language, description, content);
snippetService.update(snippet);
return "redirect:all";
}
@RequestMapping(value = "/see", method = RequestMethod.GET)
public String seeSnippet(@RequestParam("id") int id, Model model) {
model.addAttribute("delims", "\r\n");
model.addAttribute("snippet", snippetService.findById(id));
return "see";
}
@RequestMapping(value = "/remove", method = RequestMethod.GET)
public String removeSnippet(@RequestParam("id") int id, Model model) {
snippetService.remove(id);
return "redirect:all";
}
}
1t m ^E0H snippets\src\main\java\org\javamexico\snippet\web\WelcomeController.java 崀package org.javamexico.snippet.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WelcomeController {
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {
return "welcome";
}
}
t R Y Y ۯu
{_E0- snippets\src\main\webapp\META-INF\context.xml Ir
Nt Q u_E0, snippets\src\main\webapp\WEB-INF\jsp\all.jsp z#<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Snippets - All
All Snippets
ID
Title
Language
Description
Empty List!
${snippet.id}
See |
Edit |
Delete
Tt R o o Xf_E0- snippets\src\main\webapp\WEB-INF\jsp\edit.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Snippets -
Snippet
Edit Snippet
*Title: " required>
*Language:
Java
HTML
CSS
JavaScript
XML
Description: ">
*Content: "
Update Cancel
5t Q Civ_E0, snippets\src\main\webapp\WEB-INF\jsp\see.jsp *<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Snippets -
Snippet
Title:
Language:
Description:
Content:
Edit | All Snippets | Home
Zt U X X 6Fv_E00 snippets\src\main\webapp\WEB-INF\jsp\welcome.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Snippet
Snippet
Hta Zp^E0<