Skip to content

Instantly share code, notes, and snippets.

@izzuddin91
Created November 30, 2017 10:02
Show Gist options
  • Select an option

  • Save izzuddin91/ac5611e14ef91f068459bc1430fa0d93 to your computer and use it in GitHub Desktop.

Select an option

Save izzuddin91/ac5611e14ef91f068459bc1430fa0d93 to your computer and use it in GitHub Desktop.
example of passing data using Model
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to Etiqa AutoClaim webpage</h1>
<span>Please key your email and password</span> <br>
<c:url value="/showMessage" var="messageUrl"/> <br>
<c:url value="/testIzzuddin" var="messageUrl2" /> <br>
<!--form:form action="${messageUrl}" id="test" method="post"-->
<form:form action="${messageUrl}" modelAttribute="login" method="post">
<!--input type="text" name="q" class="form-control" placeholder="username"><br><br>
<input type="text" name="q" class="form-control" placeholder="password"> <br-->
<!--input type="text" name="userid"/>
<input type="text" name="passcode"/-->
<form:input path="username"/>
<form:input path="passcode"/>
<input type="submit"/>
</form:form>
<a href="${messageUrl}">Click to enter</a>
<a href="${messageUrl2}">Click to enasdasdter</a>
</body>
</html>
package com.spring.model;
public class Login {
private String username;
private int passcode;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPasscode() {
return passcode;
}
public void setPasscode(int passcode) {
this.passcode = passcode;
}
}
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome </title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
package com.spring.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.model.Login;
@Controller
public class TestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) throws IllegalArgumentException, IOException {
System.out.println("In Index method");
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
try {
String dirToPython = new String("D:\\Python3.6.3\\Python36\\python ");
String pythonScript = new String("D:\\Python3.6.3\\Python36\\classify.py ");
String dirToImage = new String("D:\\Python3.6.3\\Python36\\crack1.jpg ");
String dirToImage2 = new String("\"https://www.thesun.co.uk/wp-content/uploads/2017/03/nintchdbpict000311936030.jpg\"");
String trainingLabels = new String("D:\\Python3.6.3\\Python36\\trained_labels.txt ");
String trainingGraph = new String("D:\\Python3.6.3\\Python36\\trained_graph.pb");
Process q = Runtime.getRuntime().exec(dirToPython + pythonScript + dirToImage + " D:\\Python3.6.3\\Python36\\logs\\trained_labels.txt D:\\Python3.6.3\\Python36\\logs\\trained_graph.pb");
// Process q = Runtime.getRuntime().exec(cmd);
final int exitVal = q.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(q.getInputStream()));
System.out.println(stdInput.readLine() );
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("login", new Login());
model.addAttribute("serverTime", formattedDate );
return "index";
}
@RequestMapping(value = "/showMessage", method = RequestMethod.POST)
// public String showMessage(Locale locale, Model model, @RequestParam("userid") String username, @RequestParam("passcode") int pass) {
public String showMessage(Locale locale, Model model,@ModelAttribute("login") Login user, BindingResult result) {
if(result.hasErrors()) {
System.out.println("Error");
}
System.out.println("sadasdasda");
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
// model.addAttribute("message", formattedDate+", Hello "+username +" with passcode "+pass);
model.addAttribute("message", formattedDate+", Hello "+user.getUsername() +" with passcode "+user.getPasscode());
return "showMessage";
}
@RequestMapping(value = "testIzzuddin", method = RequestMethod.GET)
public String testIzzuddin(Locale locale, Model model) {
Date date = new Date();
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateformat.format(date);
String testString = "test string";
model.addAttribute("message", testString);
model.addAttribute("date", formattedDate);
return "testIzzuddin";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment