【问题标题】:Spring data passingSpring数据传递
【发布时间】:2017-02-09 14:28:47
【问题描述】:

我是 spring 新手,我正在关注这个创建项目教程tutorial link

但是我的jsp不知道传递的数据。

这是我的 hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> 
      votre message est : ${message}
</body>
</html>

还有我的 helloController.java

package web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class helloController {
@RequestMapping("/hello")
public ModelAndView helloWorld(){
  String message="Bonjour";
  return new ModelAndView("hello","message",message);
   }    
 }

还有我的 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>projetSpring</display-name>
  <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>
  <servlet>
     <servlet-name>dispatcher</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

还有我的 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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:jee="http://www.springframework.org/schema/jee"
	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-4.0.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">
    <context:component-scan base-package="web"/>
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix"><value>/jsp/</value></property>
          <property name="suffix"><value>.jsp</value></property>      
    </bean>
</beans>

这是我的项目目录和 hello.jsp 运行结果:

Project directory + run result

【问题讨论】:

  • 试试这个:localhost:8080/projetSpring/hello.htm
  • 尝试将目录jsp/hello.jspWebContent移动到WEB-INF文件夹(WEB-INF\jsp\hello.jsp),并修改dispatcher-servlet.xml中的name=prefix属性为@987654331 @
  • 非常感谢,它适用于 localhost:8080/projetSpring/hello.htm ,但请问是关于 web.xml 中的 url-pattern 还是没有关系?
  • 是的没错,“/jsp/hello.jsp”不是这个模式*.htm的有效表达式

标签: javascript java spring jsp spring-mvc


【解决方案1】:

实际上,您是使用此 url 直接访问您的 jsp 页面:localhost:8080/projetSpring/jsp/hello.jsp

您没有经过您的控制器,您需要使用以下 url:localhost:8080/projetSpring/hello.htm 来调用您的 DispatcherServlet 并经过您的控制器

【讨论】:

  • 改进:尝试将您的 JSP 移动到 WEB-INF 中,使其无法供外部使用。
猜你喜欢
  • 1970-01-01
  • 2013-06-17
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
相关资源
最近更新 更多