Spring MVC简单的HelloWorld例子

时间:2024-01-19 11:15:26

1.web.xml配置(主要配置Servlet)[默认情况 Spring的配置文件在WEB-INF的<servlet-name>-servlet.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring-web01</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>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2.Spring配置文件

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 使用注解来请求映射 -->
<mvc:annotation-driven/>   <!-- 配置自动扫描包 -->
<context:component-scan base-package="com.cc8w.Controller"></context:component-scan> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 重定向是否添加上下文路径-->
<property name="redirectContextRelative" value="true"></property>
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

3.控制器写法

package com.cc8w.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/HelloWorld")
public class HelloWorld { @RequestMapping("/hi.do")
public String hi()
{
System.out.println("hi.dodo");
return "hi";
}
}

4.请求结果

Spring MVC简单的HelloWorld例子

类也可以这样

package com.cc8w.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/HelloWorld")
public class HelloWorld { @RequestMapping("/hi.do")
public String hi(HttpServletRequest request,HttpServletResponse response,Model model)
{
System.out.println("hi.dodo");
System.out.println(request.getMethod()); model.addAttribute("hi", "hi123456");
return "hi";
}
}

返回模板 : https://www.cnblogs.com/fps2tao/p/7274173.html

注意, 还要配置下日志.

https://www.cnblogs.com/fps2tao/p/12809867.html