为什么我的网址localhost:8080 //// ProjectCtxt / mvc / template有效?

时间:2014-09-28 00:10:45

标签: java spring web-services spring-mvc web

以下是我的春季MVC网络应用程序。

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中的搜索按钮时,我的网址变为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>

1 个答案:

答案 0 :(得分:1)

关于/字符: Tomcat只是忽略了额外的/字符。只有上下文路径之后的部分才会路由到您的应用程序。每个Web服务器都可以自行决定如何处理它,因此Google采用不同的方式进行处理,无法进行比较。

关于在您自己的域上托管: 这通常使用前面的单独服务器完成,而不是直接使用Tomcat。 例如,使用nginx:Nginx configuration to pass site directly to tomcat webapp with context

关于表单操作中的错误网址: 由于您使用纯HTML,因此未转换为/ ProjectCtxt / search的实际位置 - 即使@RequestMapping引用/搜索也是如此。

使用标准JSTL Core,“c:url”标记将自动添加上下文路径。 另一种选择是使用Spring Form标签,它会发布到bean并自动生成正确的路径。