0

I am developing a Spring application that will do a basic CRUD operation.The login and after login the population of information is working properly.But when i trying to forward to a page from the main page i.e from where all the information is coming.The URL that is coming is strange and i can not find a reason behind that.i am posting my full code here...

web.xml



 <display-name>SpringWebCrudExample</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
   <url-pattern>/forms/*</url-pattern>
</servlet-mapping>

  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 </web-app>

dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

 ">

<!-- Enable annotation driven controllers, validation etc... -->

<mvc:annotation-driven />

<context:component-scan base-package="com.gamma.controller" />

<bean id="viewResolver"

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">

        <value>/WEB-INF/views/</value>

    </property>

    <property name="suffix">

        <value>.jsp</value>

    </property>

</bean>
</beans>

index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

login.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>Insert title here</title>
</head>
<body>
    Login Page
    <form:form action="forms/doLogin" commandName="loginForm">

    <table>

        <tr>
            <td>UserName:</td>
            <td><form:input path="username" /></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><form:input path="password" /></td>
        </tr>
        <tr>

            <td><input type="submit" value="Submit" /></td>

        </tr>
    </table>

</form:form>
</body>
</html>

mainpage.jsp

<%@ 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>Insert title here</title>
</head>
<body>
 main page is here all details will be shown here...
<c:url var="addUrl" value="/add" />
<table style="border: 1px solid; width: 500px; text-align:center">
   <thead style="background:#fcf">
    <tr>
     <th>Firstname</th>
    <th>Lastname</th>
    <th>Address</th>
    <th colspan="3"></th>
    </tr>
    </thead>
       <td><a href="${addUrl}">Add</a></td>
   <tbody>
   <c:forEach items="${mainpage}" var="student">
    <tr>
     <td><c:out value="${student.fname}" /></td>
    <td><c:out value="${student.lname}" /></td>
    <td><c:out value="${student.address}" /></td>

    </tr>
   </c:forEach>
    </tbody>
  </table>
  </body>
 </html>

AppController

@Controller
public class AppController {

public static DbImpl dbImpl;

@RequestMapping(value = "/start", method = RequestMethod.GET)
public static ModelAndView getAllinfo() {

    ModelAndView mav = new ModelAndView("/mainpage");
    System.out.println("mainpage is here");
    List<StudentVO> allInfo = dbImpl.populateInfo();
    System.out.println("The List is " + allInfo.size());
    mav.addObject("mainpage", allInfo);

    return mav;
}

@RequestMapping(value = "/doLogin", method = RequestMethod.GET)
public String ShowForm(Map model) {
    LoginForm loginForm = new LoginForm();
    model.put("loginForm", loginForm);
    return "login";
}

@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
public ModelAndView ProcessForm(@Valid LoginForm loginForm,
        BindingResult result, Map model) {

    String userName = "Jeet";
    String passWord = "gamma";
    if (result.hasErrors()) {
        return new ModelAndView("login", "loginDetails", loginForm);
    }

    loginForm = (LoginForm) model.get("loginForm");
    if (!loginForm.getUsername().equals(userName)
            || !loginForm.getPassword().equals(passWord)) {

        return new ModelAndView("login", "loginDetails", loginForm);
    }

    model.put("loginForm", loginForm);
    System.out.println("----->" + loginForm.getUsername());
    RedirectView redirectView = new RedirectView("start", true);

    return new ModelAndView(redirectView);
}


@RequestMapping(value="/add",method=RequestMethod.GET)
public String aMethod2insert(Map model){

    StudentVO studentVO=new StudentVO();
    model.put("studentVO", studentVO);
    return "insertpage";
    }

Now the problem is that when i am doing the login it is working fine and it is generating the URL SpringWebCrudExample/forms/start and coming to this page (the picture i attached hereenter image description here).Now i have added a link call add in this page from which i will go to a page where i will insert some vlaues, but the problem is here when i click on this URL it is giving SpringWebCrudExample/add this URL.As the result the page is not coming.

lucifer
  • 2,297
  • 18
  • 58
  • 100
  • I can not understand why it is not getting the full URL while clicking on the Add button – lucifer Feb 03 '14 at 10:04
  • You told it to be like that. You defined the variable `addUrl` to be `/add` which means absolute i.e. from the root of the application. – M. Deinum Feb 03 '14 at 10:06
  • then how can i solve that??i want to forward to a page of action add – lucifer Feb 03 '14 at 10:07
  • Include the proper path or use springs url tag to generate the URL. – M. Deinum Feb 03 '14 at 10:08
  • @M.Deinum if i include forms/add in the addUrl value then the url i am getting is http://localhost:9080/SpringWebCrudExample/forms/forms/add which also not working i need http://localhost:9080/SpringWebCrudExample/forms/add this URl to be generated – lucifer Feb 03 '14 at 10:09
  • Either use 'add' (relative) or `/forms/add` (absolute). Notice the missing and included '/', adding or leaving it out makes a huge difference, as you already noticed. – M. Deinum Feb 03 '14 at 10:29

1 Answers1

0

It has nothing to do with spring but with basic URL generation.

When you are prefixing a href with a / it means that this is an absolute URL and it will be navigated from the root of your application. If you leave it out it will be relative to the current URL.

Now lets take an application deployed at /app which has a servlet mapped at /servlet. If you are in a page at /app/servlet/page and you have a href like /foo it will result in the actual location of /app/foo. Leaving the / would lead to /app/servlet/foo.

For more information see Absolute vs relative URLs

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224