简体   繁体   English

如何使用Spring Maven运行Hello World程序?

[英]How to run hello world program using spring maven?

I have started to learn spring and trying to create simple hello world application using maven in sts. 我已经开始学习spring并尝试在sts中使用maven创建简单的hello world应用程序。 Please anyone what am doing wrong in this code? 请任何人在这段代码中做错了什么?

index.jsp : 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 : 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 : 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 : 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";
     }
}

This is my code and when am trying to run this in browser i got message "The requested resource is not available." 这是我的代码,当尝试在浏览器中运行此代码时,出现消息“请求的资源不可用”。 Please anyone tell me what i have done wrong ?Thanks 请有人告诉我我做错了吗?谢谢

A correction is required in dispatcher-servlet.xml. 在dispatcher-servlet.xml中需要更正。 Instead of using suffix as 'index.jsp' it should be '.jsp'. 不应将后缀用作“ index.jsp”,而应使用“ .jsp”。 Second you should also point to the view type for example JSTLView. 其次,您还应该指向视图类型,例如JSTLView。 Here is a snapshot for your reference: 这是快照供您参考:

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

I am assuming that your controller class is in package 'com.helloworld' and you are hitting a URL similar to hostname:port/AppName/hello Where AppName is your Application Name, hostname could be localhost and port be default is 8080. 我假设您的控制器类位于包'com.helloworld'中,并且您命中类似于URL的主机名:port / AppName / hello其中AppName是您的应用程序名称,主机名可以是localhost,端口默认是8080。

Change your index.jsp as index.jsp更改为

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

modify dispatcher-servlet as 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 will resolve the view's name /hello as Spring会将视图的名称/ hello解析为

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

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

I hope this solves your problem. 我希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM