端口号在URL中消失

时间:2017-03-27 19:10:33

标签: java spring spring-mvc tomcat

我的网络应用程序前几天在我的本地服务器上运行正常,表现得应该如此但现在发生了一些奇怪的事情,据我所知,我没有做任何改变。当我选择在本地服务器上运行应用程序时,页面将显示正常。但是当我点击一个按钮时,它应该基本上发回一些数据并用新信息刷新页面,这就是之前发生的事情。现在它将我重定向到http://localhost/FirstSpringMVCProject/而不是http://localhost:8080/FirstSpringMVCProject/,我收到了错误。如何解决这个问题?以下是相关代码..

控制器

@Controller
public class HelloController {

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

        ModelAndView model = new ModelAndView("DisplayMatchup");
        model.addObject("matchup", new Matchup());

        return model;
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView update(@ModelAttribute("team") String s) {
        Util.updateParse(s);
        ModelAndView model = new ModelAndView("DisplayMatchup");
        model.addObject("matchup", new Matchup());

        return model;
    }
}

Spring servlet调度程序

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


  <context:component-scan base-package="com.gontuseries.hellocontroller" />
 <mvc:annotation-driven/>

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
 </bean>


</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Web projects</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <welcome-file-list>
        <welcome-file>DisplayMatchup.jsp</welcome-file>
    </welcome-file-list>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

查看(DisplayMatchup.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<style>
ol {
    padding: 12px;
    background-color: #eee;
    display: inline-block;
    list-style-type: none;
    text-align: center;
}
body {
    font-family: Courier;
    text-align: center;
}
button {
    background-color: #4CAF50;
    border: none;
    color: white;
    padding: 12px 25px;
    text-align: center;
    font-size: 18px;
    font-family: Courier;
    white-space: normal;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basketball Player Rater</title>
</head>
<h2>Basketball Player Rater</h2>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://results.cae10gzzpyrn.us-west-2.rds.amazonaws.com:3306/results"
     user="user"  password="pass"/>
<sql:query dataSource="${snapshot}" var="result">
SELECT * FROM records ORDER BY wins DESC;
</sql:query>
<ol>
<h2>Team 1</h2>
<form:form method="post" action="http://localhost/FirstSpringMVCProject/" modelAttribute="team">
<button type="submit" name="team" value="${matchup.teams[0]}${matchup.teams[1]}"/>${matchup.teams[0]}</button>
</form:form>
</ol>
<ol>
<h2>Team 2</h2>
<form:form method="post" action="http://localhost/FirstSpringMVCProject/" modelAttribute="team">
<button type="submit" name="team" value="${matchup.teams[1]}${matchup.teams[0]}"/>${matchup.teams[1]}</button>
</form:form>
</ol>
<h3>Results:</h3>
<c:forEach var="row" items="${result.rows}">
<c:out value="${row.player}"/> --- <c:out value="${row.winrate}"/>
</br>
</c:forEach>
</body>
</html>

谢谢,如果有更好的方法来做我想做的事情,那么我愿意接受建议。

1 个答案:

答案 0 :(得分:1)

快速解决方法是将端口添加到表单的action属性中。

将其更改为

action="http://localhost:8080/FirstSpringMVCProject/"
                        ^^^^^

但是,最好使用relative url。它将类似于:

action="/FirstSpringMVCProject/"