以下是我的 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=
它可以工作吗?为什么是这样?如何修复 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>
标题.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 只是忽略多余的/字符。仅上下文路径之后的部分会路由到您的应用程序。由每个网络服务器决定如何处理它,因此 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 并自动生成正确的路径。
关于java - 为什么我的网址 localhost :8080////ProjectCtxt/mvc/template works?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26080364/