Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- buildscript {
- ext {
- springBootVersion = '2.1.2.RELEASE'
- }
- repositories {
- mavenCentral()
- }
- dependencies {
- classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
- }}
- apply plugin: 'eclipse'
- apply plugin: 'java'
- apply plugin: 'org.springframework.boot'
- apply plugin: 'io.spring.dependency-management'
- group = 'com.bill'
- version = '0.0.1-SNAPSHOT'
- sourceCompatibility = '1.8'
- repositories {
- mavenCentral()
- maven { url 'https://repo.spring.io/milestone' }
- }
- ext {
- set('springCloudServicesVersion', '2.1.0.RELEASE')
- set('springCloudVersion', 'Greenwich.RELEASE')
- }
- dependencies {
- implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
- implementation 'org.springframework.boot:spring-boot-starter-validation'
- implementation 'org.springframework.boot:spring-boot-starter-web'
- implementation 'org.springframework.boot:spring-boot-actuator'
- /*implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-config-client'
- /*implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-service-registry'
- implementation 'org.springframework.cloud:spring-cloud-starter'
- /*implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
- implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
- implementation 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'
- */
- runtimeOnly 'mysql:mysql-connector-java'
- testImplementation 'org.springframework.boot:spring-boot-starter-test'
- }
- dependencyManagement {
- imports {
- mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:${springCloudServicesVersion}"
- mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
- }
- }
- spring.mvc.view.prefix:/WEB-INF/jsp/
- spring.mvc.view.suffix:.jsp
- spring.jpa.hibernate.ddl-auto=update
- spring.datasource.url=jdbc:mysql://localhost:3306/mh1
- spring.datasource.username=root
- spring.datasource.password=root
- package com.javainuse;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
- import com.javainuse.BankAccountApplication;
- import com.javainuse.User;
- import com.javainuse.UserDAO;
- import com.javainuse.UserForm;
- @Controller
- public class MainController {
- @Autowired
- UserDAO userDAO;
- @RequestMapping("/welcome")
- public ModelAndView viewHome() {
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.setViewName("welcome");
- return modelAndView;
- }
- @RequestMapping("/users")
- public String viewUsers(Model model) {
- List<User> list = UserDAO.getUsers();
- model.addAttribute("users", list);
- return "usersPage";
- }
- @RequestMapping("/registerSuccessful")
- public String viewRegisterSuccessful() {
- return "registerSuccessfulPage";
- }
- @RequestMapping(value = "/register", method = RequestMethod.GET)
- public String viewRegister(Model model) {
- UserForm form = new UserForm();
- model.addAttribute("userForm", form);
- return "registerPage";
- }
- public String saveRegister( Model model, //
- @ModelAttribute("userForm") @Validated UserForm userForm, //
- BindingResult result, //
- final RedirectAttributes redirectAttributes) {
- // Validate result
- if (result.hasErrors()) {
- return "registerPage";
- }
- User newUser= null;
- try {
- newUser = userDAO.createUser(userForm);
- }
- // Other error!!
- catch (Exception e) {
- model.addAttribute("errorMessage", "Error: " + e.getMessage());
- return "registerPage";
- }
- redirectAttributes.addFlashAttribute("newUser", newUser);
- return "redirect:/registerSuccessful";
- }
- }
- package com.javainuse;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- @Configuration
- @ComponentScan(basePackages="...package name...")
- @EnableWebMvc
- public class MvcConfiguration extends WebMvcConfigurerAdapter{
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("WEB-INF/jsp/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- }
Add Comment
Please, Sign In to add comment