如何使用spring maven运行hello world程序?

时间:2014-11-13 10:34:48

标签: java spring jsp maven spring-mvc

我已经开始学习spring并尝试在sts中使用maven创建简单的hello world应用程序。 请问任何人在这段代码中做错了什么?

index.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>Spring4 MVC -HelloWorld</title>
</head>
<body>
<h1>Hello : ${name}</h1>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>    
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">       
 <display-name>Archetype Created Web Application</display-name>

 <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>

 <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>
</web-app>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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="com.helloworld" />     
 <bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
 <value>/WEB-INF/views/</value>
 </property>
 <property name="suffix">
 <value>index.jsp</value>
 </property>
 </bean>
</beans>

HelloWorldController.java:

@Controller
public class HelloWorldController {     
    /**
     * @param name
     * @param model
     * @return string
     */
    @RequestMapping("/hello")
     public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
     model.addAttribute("name", name);
     return "helloworld";
     }
}

这是我的代码,当我尝试在浏览器中运行时,我收到消息&#34;请求的资源不可用。&#34;请有人告诉我我做错了什么?谢谢

2 个答案:

答案 0 :(得分:0)

dispatcher-servlet.xml中需要进行更正。而不是使用后缀作为&#39; index.jsp&#39;它应该是&#39; .jsp&#39;。其次,您还应该指向视图类型,例如JSTLView。以下是供您参考的快照:

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

我假设你的控制器类在包中&#39; com.helloworld&#39;你正在点击类似的URL 主机名:端口/ AppName的/你好 其中AppName是您的应用程序名称,主机名可以是localhost,默认端口是8080。

答案 1 :(得分:0)

index.jsp 更改为

<body>
    <center>
        <h2>Hello World</h2>
        <h3>
            <a href="hello?name=ABC">Click Here</a>
        </h3>
    </center>
</body>

dispatcher-servlet 修改为

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Spring会将视图名称 / hello 解析为

When request for /hello comes to controller it will redirect to helloworld.jsp.

prefix + view name + suffix = /WEB-INF/pages/helloworld.jsp

我希望这能解决你的问题。