簡體   English   中英

為什么我的網址localhost:8080 ///// ProjectCtxt / mvc / template有效?

[英]Why does my url localhost:8080////ProjectCtxt/mvc/template works?

以下是我的Spring MVC Web應用程序。

1)我不知道為什么我的應用程序在端口號后帶有四個正斜杠的原因。 如何解決這個問題。

調度員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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" />

  <!--   <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" /> -->

      <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/tiles.xml</value>                       
                </list>
            </property>        
        </bean>

       <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>

        <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass">
                <value>
                    org.springframework.web.servlet.view.tiles2.TilesView
                </value>
            </property>
        </bean>  

</beans>

appContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- MVC context is separate from the app context usually. -->

    <!-- component scan is not recursive -->
    <context:component-scan base-package="com.ProjectCtxt.www.service" />

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="tableName" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>

web.xml中

<web-app version="2.2" id="WebApp_ID">

    <!-- <display-name>Archetype Created Web Application</display-name> -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/mvc/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

ProjectController.java

package com.ProjectCtxt.www.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ProjectCtxt.www.service.ProjectService;

@Controller("resultController")
public class ResultController {

    private final ProjectService projectService;

    @Autowired
    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }


    @RequestMapping(value ="/template", method = RequestMethod.GET)
    public String getPersonList(ModelMap model) {

        return   "header";
    }

    @RequestMapping(value ="/search", method = RequestMethod.GET)
    public String getStudentResult(String rNo, ModelMap model){
        return "numberResult";
    }
}

2)同樣,當我在本地計算機上處​​理項目時,我總是使用loclahost:8080 / ProjectCtxt / mvc / template,但是在生產環境中部署時,我希望它是mywebsite.com。 我怎樣才能做到這一點?

2a)當我嘗試www.google.com////calendar/render ,它可以工作。 但是,當我輸入www.google.com/calendar////render它不起作用。

3)如果我使用localhost:8080 / ProjectCtxt / mvc / template,則轉到主頁。 當我單擊jsp中的“搜索”按鈕時,我的URL變為http://localhost:8080/search?regNo 但是,如果我手動將localhost:8080/search?regNo=更改為localhost:8080/ProjectCtxt/mvc/search?regNo=可以嗎? 為什么是這樣? 如何將網址固定為localhost:8080/ProjectCtxt/mvc/search?regno=而不是手動輸入“ ProjectCtxt / mvc / search?regNo =“

tiles.xml

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

<!DOCTYPE tiles-definitions PUBLIC    
  "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"   
     "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">


<tiles-definitions>
    <definition name="template" template="/WEB-INF/jsp/template.jsp">
        <put-attribute name="title" value="Lets see"/>
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
        <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp  "/>
        <put-attribute name="center" value="/WEB-INF/jsp/ads.jsp" />
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /> 
    </definition>

    <definition name="header" extends="template">
        <put-attribute name="title" value="" />  
        <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp" />  
    </definition>


    <!-- <definition name="bottom" extends="new.template">
        <put-attribute name="bottom" value="/mvc/jsp/ads.jsp" />
        <put-attribute name="bottom" value="/WEB-INF/jsp/ads.jsp" />
    </definition> -->

</tiles-definitions>

header.jsp中

<%-- <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<!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=UTF-8">
    <title>Insert title here</title>
    </head>

    <body>
    <center>
    <h1>Header</h1></center>


        <tiles:insertAttribute name="body" />
     </body>
 </html> --%>



    <form action="/search" method="GET" >
        <div style="text-align: center;">
            <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" placeholder="ABCD"></input> or 
            <input  id="collName" type="text" size="30" maxLength="50" placeholder="EFGH"></input>
        </div>
        <div style="text-align: center;">
        <input type="submit"  value="search"><br/>

        </div>




    </form>

關於/字符:Tomcat只忽略了多余的/字符。 僅上下文路徑之后的部分被路由到您的應用程序。 由每個Web服務器決定如何處理它,因此Google采取不同的方式,這是無法比較的。

關於在您自己的域上托管:通常是在前面的單獨服務器上完成,而不是直接在Tomcat上完成。 例如,使用nginx: Nginx配置可通過上下文將網站直接傳遞到tomcat Webapp

關於表單操作中不正確的URL:由於您使用的是純HTML,因此不會轉換為/ ProjectCtxt / search的實際位置-即使@RequestMapping引用/ search也是這種情況。

使用標准的JSTL Core,“ c:url”標記將自動添加上下文路徑。 另一種選擇是使用Spring Form標簽,該標簽發布到Bean並自動生成正確的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM