Spring MVC application not working - spring

I am trying as explained here, but couldn't get the output that is shown for the second page.
It redirects to the second page but doesn't print the text i entered, rather returns the empty page.
Any suggestions ? i am using spring framework 3.0.2.
helloView.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1>${helloMessage}</h1>
</body>
</html>
nameView.jsp
<%#taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Name</title>
</head>
<body>
<h1>Enter your Name</h1>
<spring:nestedPath path="name">
<form action="" method="post">
Name:
<spring:bind path="value">
<input type="text" name="${status.expression}" value="${status.value}">
</spring:bind>
<input type="submit" value="OK">
</form>
</spring:nestedPath>
</body>
</html>

Add this bean class
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
This bean is responsible for automatically creating an URL mapping for all controllers registered in the file. It takes the fully-qualified class name of the controller (in our case, controller.HelloController) and strips the package name and Controller suffix, then uses the result as a URL mapping.

Related

jsp files not getting updated

I'm going to create a website using Spring.
So I created a jsp file, but no matter how I change the source code from today, the jsp file will not be updated.
This is my jsp code
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
</head>
<body id="page-top">
<!-- jQuery 사용을 위한 include, 항상 최상단에 위치해 있어야 함 -->
<jsp:include page="common/jQuery.jsp"/>
<!-- servletPath가 만약 true라면 아래 진입 -->
<!-- Page Wrapper -->
<div id="wrapper">
<!-- 사이드 네비바 파일을 가져와서 include 시켜줌, 사이드 네비바는 무조건 div id 가 wrapper안에 있어야 함 -->
<jsp:include page="common/side_nav.jsp"/>
<c:if test="${ pageName eq 'noticePage' }">
<script>
$(document).ready(function() {
$("#accordionSidebar").children(".nav-item").eq(0).removeClass("active");
$("#accordionSidebar").children(".nav-item").eq(2).addClass("active");
$('#summernote').summernote({
height: 500,
lang: "ko-KR"
});
});
</script>
</c:if>
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Top bar 부분을 가져와서 사용함, Top bar는 무조건 content안에 있어야 함-->
<jsp:include page="common/top_bar.jsp"/>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h4 class="m-0 font-weight-bold text-primary">공지사항항항</h4>
</div>
<div class="card-body" style="height:auto">
<form method="post">
<input type="text" placeholder="제목"/>
<div id="summernote"></div>
</form>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<!-- Footer 부분 이건 content-wrapper 제일 아래에 위치시키도록 하자-->
<jsp:include page="common/footer.jsp"/>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- 맨위로 올라가게 해주는 토글 버튼과 우측 상단로그아웃 부분. 이건 항상 body의 제일 아래부분에 있도록 위치시키자 -->
<jsp:include page="common/logout_scroll.jsp"/>
<!-- summernote cdn 시작-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.js"></script>
<!-- summernote cdn 끝 -->
</body>
</html>
I checked the page source on Google Chrome, but the tag was not added to the source after rebooting the local server many times.
I searched a lot and found that source freezing occurred.
So I checked the work file in the following path, but the work file is empty.
C:\dev\apache-tomcat-9.0.58-windows-x64\apache-tomcat-9.0.58\work
Also, I checked the work file in the following path, but it is also empty.
C:\workspace3\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work
How can I solve this problem?

Not able to load partial templates using Thymleaf

I have templates/header.html with following code
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="testing" th:fragment="header">
2016 Header
</div>
</body>
</html>
I have templates/index.html with following code
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div th:insert="header :: header"></div>
<h3>User Registration</h3>
<form action="/AddUser" method="post">
<input type="text" name="fname" id="lname" placeholder="Firstname"
required /> <input type="text" name="lname" id="fname"
placeholder="Lastname" required />
<button type="submit">Insert</button>
</form>
</body>
</html>
I am expecting 2016 Header in index.html. But, nothing is getting displayed.
I am using following configuration in my gradle dependencies
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
Please tell me where i am going wrong.

Spring + Thymeleaf not able to populate String on render

I have a testt.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Advanced Test</title>
</head>
<body>
<form action="#" th:action="#{getClasses}" method="GET">
<br />
<br />
<p style="margin-left: 35px">Submit fully classified package names</p>
Class Name: <input type="text" name="class_name" size="90px"></input>
<input type="text" name="classes" size="90px" th:field="${foo}"></input>
<input type="submit" value="Submit" ></input> <br />
</form>
</body>
</html>
And have the following code in the controller method:
model.addAttribute("foo", "foo");
return "testt";
Why "foo" is not populating in the html? If instead of String i add an object and try to get its variable it works fine.
i think if you want use it like this you should use th:value instead of th:field
You need this th:field=__${foo}__,
have a look at the doc here:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html

unable to pass variables between jsp templates

I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.
a.jsp
start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
stop including
b.jsp
${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>
output
start including
null
stop including
You have to set your param before include.
start including
<jsp:params>
<jsp:param name="somevar" value="zzzz" />
</jsp:params>
<jsp:include page="b.jsp">
</jsp:include>
stop including
And can u try this in b.jsp:
<%=request.getParameter("somevar");%>
What is output?
Try the following code:
home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<jsp:include page="b.jsp">
<jsp:param name="something" value="something" />
</jsp:include>
<h1>Bye World</h1>
</body>
</html>
In the b.jsp page use the following code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Param example</title>
</head>
<body>
<h1>
<c:out value="${param.something}" />
</h1>
</body>
</html>
The output must be
Hello world!
something
Bye World
Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?

How do I prevent an Ajax alert from rendering the layout in CakePHP?

I tried to retrieve the value of a particular variable and alert it.
That is,I wanted to get the value "{"attributes":[{"type":"Text","labels":"Untitled1"}]}
" in that variable. Using the success: function(msg) in ajax ,I alerted the value.
But instead of the required value :{"attributes":[{"type":"Text","labels":"Untitled1"}]}
I am getting the text given below. What is the problem?
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
CakePHP: the rapid development php framework: Forms
</title>
<link href="/cake_1_2/favicon.ico" type="image/x-icon" rel="icon" />
<link href="/cake_1_2/favicon.ico" type="image/x-icon" rel="shortcut icon" />
<link rel="stylesheet" type="text/css" href="/cake_1_2/css/cake.generic.css" />
</head>
<body>
<div id="container">
<div id="header">
<h1>
CakePHP: The rapid development php framework
</h1>
</div>
<div id="content">
{"attributes":[{"type":"Text","labels":"Untitled1"}]}
</div>
<div id="footer">
<a href="http://www.cakephp.org/" target="_blank">
<img src="/cake_1_2/img/cake.power.gif" alt="CakePHP: The rapid development php framework" border="0" />
</a>
</div>
</div>
</body>
The problem is that you still use the default layout. Try switching to another shipped layout for Ajax-responses:
// In your controller
$this->layout = 'ajax';

Resources