ASP.Net MVC Book Review and musings

Written by Welzie

August 14th, 2009 at 1:27 am

Posted in .Net, Books

Tagged with , ,

With 9 comments

ASP.Net MVC musings

Book Review – Pro ASP.Net MVC Framework – Steven Sanderson

Book on Amazon
I am a Java, PHP, and .Net developer which may make my opinion worthless to some Microsoft zealots but I thought I would type up my thoughts on this book anyway. Overall I found this book to be a great way to learn ASP.Net MVC. It has great examples and enough background material to bring people new to the MVC pattern up to speed. Below are my pros and cons along with notes on chapters 1-13.

Note about the cover.

One thing I found interesting is that the cover says “Discover the biggest innovation in Microsoft web development since 1.0″. I find it amusing that something as old as MVC web frameworks can be called an innovation for development since MVC frameworks are nothing new to web development. Heck they are not even new to ASP.Net development. See MonoRail .



Chapter 1

Chapter 2

Chapter 3

  • Gives in depth definition to each part of MVC
  • Gives good definition of entities vs value objects
  • Lists design and pattern best practices
  • Brief discussion of IoC and IoC containers
  • Brief discussion on automated testing and the TDD movement
  • Gives example of creating mock implementations for testing

Chapter 4

  • Goes into medium depth while building a project with unit testing, domain model, and web layer
  • uses LING to SQL for database interactions
  • Sets up IoC with Castle Windsor
  • Shows how to use Nunit and Moq to create unit tests
  • Good definition of TDD and how the author thinks of it as actually BDD (behaviour driven development)

Chapter 5

  • Examples of how to test every part of ASP.Net MVC
  • Shows example of how to only expose an interface in your controller to avoid tying the controller to your business logic implementation

Chapter 6

  • CRuD actions and views are added to the example project
  • Example of username/password authentication
  • Shows how to upload an image through a form field

Chapter 7

  • Detail of MVC project folder structure
    • Very well done, explains the what/why for each folder
  • Goes over the naming convention which are crucial since ASP.Net MVC follows CoC

Chapter 8

  • Everything you need to know about how url routing works and also how to create links in your application to your controllers/actions

Chapter 9

  • More details on what code belongs in a controller/action
  • More details on how the views are rendered
  • Example of how to use the [Authorize] filter attribute, which is a great way to easily secure your actions individually

Chapter 10

  • Details on how views work specifically how data is passed between layers and tools available to display that data. Specifically the HTML helpers.

Chapter 11

  • Everything you need to know about how to modify and configure how data is retrieved from forms and urls and then binded to action parameters and model objects
  • Validation
    • Author basically suggest using plain c# in the model layer for validation. doesn’t list the re-usability and “all ready done for YOu” benefits of using a validation utility.

Chapter 12

  • Simple examples of how to use the AJAX HTML helpers which includes JQuery.

Chapter 13

  • Demonstrates basics of HTTP requests to help you understand how vulnerable web sites are. Really good reading for less experienced web developers.
    • Gives example of how to fake an http request
    • Gives examples of using tools like firebug and fiddler
    • Details cross-site scripting and html injection
    • Details how one of the previous chapter examples had a vulnerability and how it can be fixed

Book on Amazon


Read HTML with Java – Then 7 Fun Things to do to It

Written by MikeNereson

August 3rd, 2009 at 10:57 pm

Posted in java

Tagged with ,

With 10 comments

There are several ways to get the HTML content of a URL from Java. There are even more ways to get the HTML using open source java libraries. Last week Lars Vogel shared how to get the HTML using nothing but the SDK.


final URL url = new URL("");
final InputStream inputStream = new InputStreamReader(url);
final BufferedReader reader
             = new BufferedReader(inputStream).openStream();

String line;

while ((line = reader.readLine()) != null) {


Apache Commons HttpClient

You can also use the Apache Commons HttpClient for a slightly easier to use library.

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("");

try {

 byte[] responseBody = method.getResponseBody();

 System.out.println(new String(responseBody));

} catch (Exception e) {


} finally {


But the real fun comes after you get the HTML. Now you get to work with it.

7 Things to do with HTML Source in Java

1. Extract the Text from the Markup

I worked on a web application that was similar to a feed reader. One of the features that we supported was searching the pages. To do this I needed to extract the text from the markup. I use the CyberNeko HTML Parser for this task.

private String getHtmlFilteredString(Reader reader)

  // create element remover filter
  ElementRemover remover;
  remover = new ElementRemover();

  OutputStream stream = new ByteArrayOutputStream();

    String encoding = "ISO-8859-1";
    XMLDocumentFilter writer = new Writer(stream, encoding);

    XMLDocumentFilter[] filters = {remover, writer};

    XMLInputSource source = new XMLInputSource(null, null, null, reader, null);

    XMLParserConfiguration parser = new HTMLConfiguration();
    parser.setProperty("", filters);


  } catch (Exception e) {


  String content = stream.toString().trim();

  return content;

2. Extract Links

To find all of the links in an HTML fragment, maybe for your own spider, or to extract email addresses, you can use HtmlParser

Collection<String> links = new ArrayList<String>();

try {

  URI uriLink = new URI(url);
  Parser parser = new Parser();
  NodeList list = parser.extractAllNodesThatMatch(new NodeClassFilter (LinkTag.class));

  for (int i = 0; i < list.size (); i++){
    LinkTag extracted = (LinkTag)list.elementAt(i);
    String extractedLink = extracted.getLink();

} catch (Exception e) {


3. Change Links

Using the previous code, instead of calling getLink, you can call setLink to change the href. For example, this might be used by any type of analytics software that needs to track hits.

4. Collect Email Addresses

Using the previous code, before adding the link to the Collection, chech to see if it uses the mailto protocol by calling the boolean method isMailLink()

  for (int i = 0; i < list.size (); i++){

    LinkTag extracted = (LinkTag)list.elementAt(i);

    if (extracted.isMailLink())
      String extractedLink = extracted.getLink();

5. Collect Images

Still using HtmlParser, you can extract images by filtering on the ImageTag.

Collection<String> imageUrls = new ArrayList<String>();

try {

  URI uriLink = new URI(url);
  Parser parser = new Parser();
  NodeList list = parser.extractAllNodesThatMatch(new NodeClassFilter (ImageTag.class));

  for (int i = 0; i < list.size (); i++){
    ImageTag extracted = (ImageTag)list.elementAt(i);
    String extractedImageSrc = extracted.getImageUrl();

} catch (Exception e) {


6. Add Syntax Highlighting

If you’re going to present the source on screen you have to add syntax highlighting. If you’re displaying on a web page, I would highly recommend using something like SyntaxHighligher, which is what we use on this blog. If you are displaying an a Swing app, you can use a tool called  Syntax which is now several years old.

try {

     ToHTML toHTML = new ToHTML();

     toHTML.setInput(new FileReader(""));
     toHTML.setOutput(new FileWriter(""));


 } catch (Exception e){


7. Diff Two Sources

Once you get the HTML source of two URLs, or you have an old version of the HTML stored somewhere, you might want a nicely formatted diff output.

Madlep wrote his own method to handle the diff and output the results over on

Given some simple input, his code gives simple output.

String one = "" +
    "<ul>" +
    "  <li>item 1</li>" +
    "  <li>item 2</li>" +

String two = "" +
    "<p>This is text</p>" +
    "<ul>" +
    "  <li>item 1</li>" +
    "  <li>item 2</li>" +
    "  <li>item 3</li>" +

System.out.println(diffSideBySide(one, two));


                     >  <p>This is text</p>
<ul>                    <ul>
 <li>item 1</li>          <li>item 1</li>
 <li>item 2</li>          <li>item 2</li>
                     >    <li>item 3</li>
</ul>                   </ul>

3 More Things to do with the HTML

This list sure feels incomplete at only 7 of a nice even 10 things. What else could you you do with the HTML? Ill pick the top three from the comments and finish this blog post with them.

(read html in java) (java get html source)


Getting friendly with Spring, JUnit and EasyMock.

Written by DanEngland

August 1st, 2009 at 8:31 pm

Posted in java

Tagged with , ,

With no comments

Here are some steps that can get you using Spring, JUnit and EasyMock all together in some Test Driven Development hotness.

Start by adding the following lines to the top of your unit test. Specifying Autowire by name ensures you get the injection you want and will stop those Spring errors that there are more than one of the same type of mock objects in your mock-applicationContext.xml. When you specify the Spring Junit runner you must provide one ore more context configurations with @ContextConfiguration.

@Configurable(autowire = Autowire.BY_NAME)
@ContextConfiguration(locations = {"classpath:com/some/domain/someproject/resources/mock-applicationContext.xml"})
public class MyClassUnitTest {
     @Autowired private Collaborator mockCollaborator;

     public void setup() throws Exception {

     public void teardown() throws Exception {

     public void testMyClass() throws Exception {

          //Set your mock behavior here


          MyClass myClass = new MyClass(mockCollaborator);

          // Other JUnit assertions

Then add your mocks to your mock-applicationContext.xml (an applicationContext in your test resources just for providing your unit tests with spring injection dependencies):


<bean id="mockCollaborator" name="mockCollaborator" class="org.easymock.EasyMock" factory-method="createStrictMock">
     <constructor-arg value="com.some.domain.someproject.Collaborator"/>

<bean id="mockOtherCollaborator" name="mockOtherCollaborator" class="org.easymock.EasyMock" factory-method="createStrictMock">
     <constructor-arg value="com.some.domain.someproject.OtherCollaborator"/>

Ensure your maven pom has the following test-scoped dependency:



Then use your mocks as normal. Make sure you call EasyMock.reset(mock) in your @After teardown() method so that each of your mocks are reset for each test. You can also tell Spring to reset the context after a test if needed with @DirtiesContext.

For official documentation and tutorials check out these links.