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

1)我不知道为什么我的应用程序在端口号后使用四个正斜杠。如何解决这个问题。

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: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=,它可以工作吗?为什么是这样?我如何将URL修复为自动为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 configuration to pass site directly to tomcat webapp with context

关于表单操作中不正确的URL:
由于您使用的是纯HTML,因此不会转换为/ ProjectCtxt / search的实际位置-即使@RequestMapping引用/ search,情况也是如此。

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

09-10 01:57
查看更多