Tutorial: step by step Helloworld with Spring MVC (part 2)

Thursday, November 26th, 2015 (0 Comment)

To continue part 2, you need click here to practice do project show text Helloworld with Spring mvc.

This article, we create helloworld project mvc:

  • Create a View
  • Create a Controller
  • Create a Model
  • How to share data between View and Controller via Model

We will develop continue on helloworld project with input parameters, output parameters and know about MVC (model view controller), we build 2 pages:

  • First page: form to input username and name
  • Second page: get information from first page and show username and name
spring helloworld

spring helloworld

Step 1: customize spring structure :

  • Create or rename package demo.springmvc to com.demo.springmvc.controller: include controller files, these files only focus to control on screen
  • Create package com.demo.springmvc.entity: include entity files to storing information of object
project struct

project struct

porn.xml: add version to build with java sdk if need

Line 35: add version java sdk

Step 2: Custom project helloworld

Create UserInfo.java class in com.demo.springmvc.entity package

Modify hello.jsp file:

  • <%@taglib%> declare libraries to get tags build form
  • <form:form> create form. modelAttribute is bean object used to display information in form
  • <form:input> create input text. path=”property” is name bean’s property (UserInfo’s property)

Create hellosuccess.jsp in pages folder:

  • ${user.username}: display username property of user model

Modify HelloWordController.java class again as below

  • @Controller: this class is controller
  • @RequestMapping(value=”action”): define an action
  • model.addAttribute(name, value): add an attribute into model
  • @ModelAttribute(“user”): add user object into model
  • return ViewName: choose view to display (jsp file)

Save all, right click name project in eclipse explorer and select Build Project.

In Server tab, click Publish to The Server, finally, click run server and open browser with url http://localhost:8080/springmvc

Notes: if have ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC00001: Failed to start service jboss.web.deployment.default-host, you need to check source deploy in WEB-INF folder, lib folder, must have full lib in this folder. If empty, you must to config add lib when deploy : add deployment assembly (May be you update maven agent -> project missing config deployment assembly), you click here to review step 6 in part 1.

Tags: , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Leave a Reply

Your email address will not be published. Required fields are marked *