SpringMVC Hello(IDEA)

  以前一想要学习Java相关知识的时候,就会认为.NET FrameWork的相关技术还没学个通透还夹生饭。现在感觉只会.NET有些单一,需要多掌握几种开发技术,尽管.NET还没达到精通。貌似Hello XXX是很多程序的入门惯例。网上搜的都不全,自己摸索着来一篇Hello Spring MVC 开启我的Java之旅...

  开发工具IDEA 2017.1.2

  环境JDK1.8\Tomcat8.5.3

  项目机构

SpringMVC Hello(IDEA)

  1.新建项目

SpringMVC Hello(IDEA)

2.配置项目

SpringMVC Hello(IDEA)

3.src下新建HelloController控制器(属于页面控制器)

package com.cj.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
@RequestMapping(value="hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}

4.WEB-INF下新建文件夹jsp,jsp文件夹内新建hello.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

5.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>helloweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloweb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

6.配置helloweb-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"
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.cj"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

7.配置Tomcat,选择自己的Tomcat和JDK安装路径

SpringMVC Hello(IDEA)

SpringMVC Hello(IDEA)

  SpringMVC Hello(IDEA)

SpringMVC Hello(IDEA)

开始运行

SpringMVC Hello(IDEA)

上一篇:【.net 深呼吸】细说CodeDom(8):分支与循环


下一篇:vs2015安装体验