Merge branch 'master' of git://github.com/jgarciaheredero/tutorials into jgarciaheredero-master
# Conflicts: # spring-mvc-java/pom.xml
This commit is contained in:
commit
8a3c918b94
|
@ -250,7 +250,7 @@
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
<logback.version>1.1.5</logback.version>
|
<logback.version>1.1.5</logback.version>
|
||||||
|
|
||||||
<!-- various -->
|
<!-- various -->
|
||||||
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
|
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
|
||||||
<!-- util -->
|
<!-- util -->
|
||||||
|
@ -263,6 +263,7 @@
|
||||||
<httpcore.version>4.4.1</httpcore.version>
|
<httpcore.version>4.4.1</httpcore.version>
|
||||||
<httpclient.version>4.5</httpclient.version>
|
<httpclient.version>4.5</httpclient.version>
|
||||||
<rest-assured.version>2.9.0</rest-assured.version>
|
<rest-assured.version>2.9.0</rest-assured.version>
|
||||||
|
<net.sourceforge.htmlunit>2.23</net.sourceforge.htmlunit>
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||||
|
@ -274,4 +275,5 @@
|
||||||
<aspectj.version>1.8.7</aspectj.version>
|
<aspectj.version>1.8.7</aspectj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
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
|
||||||
|
@RequestMapping("/message")
|
||||||
|
public class MessageController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/showForm", method = RequestMethod.GET)
|
||||||
|
public String showForm() {
|
||||||
|
return "message";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/processForm", method = RequestMethod.POST)
|
||||||
|
public String processForm(@RequestParam("message") final String message, final Model model) {
|
||||||
|
model.addAttribute("message", message);
|
||||||
|
return "message";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
|
public class HtmlUnitAndJUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
||||||
|
try (final WebClient webClient = new WebClient()) {
|
||||||
|
|
||||||
|
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||||
|
|
||||||
|
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||||
|
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
|
||||||
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = { TestConfig.class })
|
||||||
|
public class HtmlUnitAndSpringTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
|
private WebClient webClient;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void givenAMessage_whenSent_thenItShows() {
|
||||||
|
final String text = "Hello world!";
|
||||||
|
HtmlPage page;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
page = webClient.getPage("http://localhost/message/showForm");
|
||||||
|
System.out.println(page.asXml());
|
||||||
|
|
||||||
|
final HtmlTextInput messageText = page.getHtmlElementById("message");
|
||||||
|
messageText.setValueAttribute(text);
|
||||||
|
|
||||||
|
final HtmlForm form = page.getForms().get(0);
|
||||||
|
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
|
||||||
|
final HtmlPage newPage = submit.click();
|
||||||
|
|
||||||
|
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
|
||||||
|
|
||||||
|
Assert.assertEquals(receivedText, text);
|
||||||
|
System.out.println(newPage.asXml());
|
||||||
|
|
||||||
|
} catch (FailingHttpStatusCodeException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
||||||
|
try (final WebClient client = new WebClient()) {
|
||||||
|
|
||||||
|
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||||
|
|
||||||
|
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||||
|
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
|
public class HtmlUnitWebScraping {
|
||||||
|
|
||||||
|
public static void main(final String[] args) throws Exception {
|
||||||
|
try (final WebClient webClient = new WebClient()) {
|
||||||
|
|
||||||
|
webClient.getOptions().setCssEnabled(false);
|
||||||
|
webClient.getOptions().setJavaScriptEnabled(false);
|
||||||
|
|
||||||
|
final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive");
|
||||||
|
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0);
|
||||||
|
|
||||||
|
System.out.println("Entering: " + latestPostLink.getHrefAttribute());
|
||||||
|
|
||||||
|
final HtmlPage postPage = latestPostLink.click();
|
||||||
|
|
||||||
|
final HtmlHeading1 heading1 = (HtmlHeading1) postPage.getByXPath("//h1").get(0);
|
||||||
|
System.out.println("Title: " + heading1.getTextContent());
|
||||||
|
|
||||||
|
final List<HtmlHeading2> headings2 = (List<HtmlHeading2>) postPage.getByXPath("//h2");
|
||||||
|
|
||||||
|
final StringBuilder sb = new StringBuilder(heading1.getTextContent());
|
||||||
|
for (final HtmlHeading2 h2 : headings2) {
|
||||||
|
sb.append("\n").append(h2.getTextContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sb.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.htmlunit;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||||
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
|
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan(basePackages = { "com.baeldung.web.controller" })
|
||||||
|
public class TestConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ViewResolver thymeleafViewResolver() {
|
||||||
|
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||||
|
viewResolver.setTemplateEngine(templateEngine());
|
||||||
|
viewResolver.setOrder(1);
|
||||||
|
return viewResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletContextTemplateResolver templateResolver() {
|
||||||
|
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||||
|
templateResolver.setPrefix("/WEB-INF/templates/");
|
||||||
|
templateResolver.setSuffix(".html");
|
||||||
|
templateResolver.setTemplateMode("HTML5");
|
||||||
|
return templateResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SpringTemplateEngine templateEngine() {
|
||||||
|
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||||
|
templateEngine.setTemplateResolver(templateResolver());
|
||||||
|
return templateEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue