Translate

Thursday, April 28, 2022

Golang - Converting from Java and JSP to Golang and a Template

I'm working on converting a Java web application to Golang. Converting the JSP to a template was fairly straight forward.
In Java, I have this function to load the form:

@RequestMapping("/valueinputDateLoadAD")
public ModelAndView valueinputDateLoadAD() {
	Map myModel = new HashMap();
	myModel.put("page", "ldap/dateLoadAD/");
	myModel.put("label1", "Filter:");
	myModel.put("title", "Last update date in the form YYYYMMDD or Res. ID");
	myModel.put("input1_title", "AD Last Update Date or Res. ID");
	return new ModelAndView("input2Form", myModel);
}

The JSP form:

<form action="${page}" name="form1" id="form1" method="POST" target="iframename" onsubmit="event.stopPropagation(); event.preventDefault();">
<table id="input2" style="vertical-align: middle;">
  <tr>
	<td></td><td colspan="3"><b>${title}</b></td>
  </tr>
  <tr>
	<td style="text-align: right;">${label1}</td>
	<td><input style="min-width: 300px;" id="value1" type="text" name="value1" title="${input1_title}" maxlength="60" onkeydown="if (event.keyCode == 13) document.getElementById('input2search').click()"/></td>
	<td>  </td>
	<td><input type="button" id="input2search" value="   Go   " onclick="event.stopPropagation(); event.preventDefault();loadBySubmit2('form1');"/>
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
	</td>
  </tr>
</table>
</form>


In Golang, I have this structure and function to load the template:

type StrcP1 struct {
	Page      string
	Label1    string
	Title     string
	In1_Title string
}
func valueinputDateLoadAD(w http.ResponseWriter, req *http.Request) { logger.NLog.Info().Msg("valueinputDateLoadAD called") strcP := StrcP1{Page: "dateLoadADGroup", Label1: "Filter:", Title: "Last update date in the form YYYYMMDD or Res. ID", In1_Title: "AD Last Update Date or Res. ID"} tmpl, err := template.ParseFiles("_resources/html/form/input2Form.html") if err != nil { logger.NLog.Error().Err(err).Msg("valueinputDateLoadAD: file parse error") http.Error(w, "Something went wrong", http.StatusInternalServerError) return } err = tmpl.Execute(w, strcP) if err != nil { logger.NLog.Error().Err(err).Msg("valueinputDateLoadAD: template execute error") http.Error(w, "Something went wrong", http.StatusInternalServerError) } }

And this is my template:

<form action="{{.Page}}" name="form1" id="form1" method="POST" target="iframename" onsubmit="event.stopPropagation(); event.preventDefault();">
<table id="input2" style="vertical-align: middle;">
  <tr>
	<td></td><td colspan="3"><b>{{.Title}}</b></td>
  </tr>
  <tr>
	<td style="text-align: right;">{{.Label1}}</td>
	<td><input style="min-width: 300px;" id="value1" type="text" name="value1" title="{{.In1_Title}}" maxlength="60" onkeydown="if (event.keyCode == 13) document.getElementById('input2search').click()"/></td>
	<td>  </td>
	<td><input type="button" id="input2search" value="   Go   " onclick="event.stopPropagation(); event.preventDefault();loadBySubmit2('form1');"/>
	</td>
  </tr>
</table>
</form>

No comments:

Post a Comment

Thank you for commenting!