Spring应用程序在两个不同的时间提供两个不同的URL

时间:2014-02-03 09:58:13

标签: java spring jsp spring-mvc servlets

我正在开发一个Spring应用程序,它将执行基本的CRUD操作。登录和登录后的信息填充正常。但是当我尝试从主页面转发到页面时,即所有信息都是即将到来的网址很奇怪,我找不到背后的原因。我在这里发布我的完整代码......

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>

调度-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";
    }

现在的问题是,当我正在进行登录时,它工作正常,它正在生成URL SpringWebCrudExample / forms / start并进入此页面(我附在此处的图片enter image description here)。现在我有了在这个页面中添加了一个链接调用添加,我将转到一个页面,我将在其中插入一些vlaues,但问题是在这里,当我点击这个URL它给SpringWebCrudExample /添加这个URL。结果页面不是来了。

1 个答案:

答案 0 :(得分:0)

它与spring无关,但基本的URL生成。

当您使用/为href添加前缀时,这意味着这是一个绝对URL,它将从您的应用程序的根目录导航。如果你把它遗漏,它将相对于当前的URL。

现在让我们在/app部署一个应用程序,该应用程序的servlet映射为/servlet。如果您位于/app/servlet/page的某个页面中并且您有/foo之类的href,则会生成/app/foo的实际位置。离开/会导致/app/servlet/foo

有关详细信息,请参阅Absolute vs relative URLs