Java Spring 3.0新手启动项目未显示值
我在NetBeans7.3中创建了简单的helloworld。但我在从控制器加载带有值的jsp时遇到了问题。我部分启发了(第141页)的指示。但当我想用控制器中设置的值加载jsp时,不会显示值。控制器中有捕获-获取规则。我输入了这个网址:Java Spring 3.0新手启动项目未显示值,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我在NetBeans7.3中创建了简单的helloworld。但我在从控制器加载带有值的jsp时遇到了问题。我部分启发了(第141页)的指示。但当我想用控制器中设置的值加载jsp时,不会显示值。控制器中有捕获-获取规则。我输入了这个网址:http://localhost:8080/HelloWorld3/hello.htm。我得到jsp,但未显示值。当我输入上面教程中所述的url时: http://localhost:8080/HelloWeb/hello,我收到404错误,找不到此页面。 我必
http://localhost:8080/HelloWorld3/hello.htm
。我得到jsp,但未显示值。当我输入上面教程中所述的url时:
http://localhost:8080/HelloWeb/hello
,我收到404错误,找不到此页面。
我必须输入什么url或我做了什么?我猜这个错误在某个配置文件中。Thx
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cz.ryska.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp:
<%@page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello!!!!!!</h1>
<h2>${message}</h2>
</body>
</html>
JSP页面
你好
${message}
试试这个
@Controller
@RequestMapping("/hello")
public class MyController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
hello.jsp
<html>
<body>
<h1>Message : ${message}</h1>
</body>
消息:${Message}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.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>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="hello.htm">helloController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="helloController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="hello" />
</beans>
<?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"
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.xsd">
<context:component-scan base-package="com.gemini.spring.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
您这样编写helloWorld应用程序有什么特殊原因吗? 对于一个简单的helloWorld应用程序,它似乎不必要地复杂 首先,除非您有这样做的特定原因,否则实际上不需要定义两个单独的控制器 我还研究了您链接的tutorialspoint示例,我的示例运行得非常好,但它是原始示例的克隆 如果你想扩展或玩转这个例子,我建议你首先克隆这个例子,让它工作起来。。。然后从那里往前走,一步一步
虽然我面前没有我的解决方案,但Georgy Gobozov给出的答案看起来是正确的。我在Netbeans中创建了新项目,并复制了您的代码。我在实际控制器包上更改了基本包。我在Netbeans 7.3中使用Glassfish 3.1.2。但我得到警告:在名为“dispatcher”的DispatcherServlet中找不到URI为[/HelloWorld4]的HTTP请求的映射。在启动项目后,我得到404错误。若要调用控制器,您应该打开localhost:8080/上下文路径(若有)/hello@OndřejRyška首先试着读一下:@Georgy Gobozov context path是应用程序名,好吗?我的应用程序名为HelloWorld4。我输入,但在netbeans glassfish控制台中出现错误:严重:PWC6117:文件“C:\JavaProgramy\Spring\HelloWorld4\build\web\jsp\hello.jsp”不是found@ZagorulkinDmitry好的,谢谢,这篇教程看起来不错,我会读这篇。你在这篇教程中使用的是Netbeans还是Eclipse?
<?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"
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.xsd">
<context:component-scan base-package="com.gemini.spring.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>