Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> Spring MVC 入门 Hello wrold

Spring MVC 入门 Hello wrold

77900 开源 | 2015-3-17

向LIB中需要的jar包:

spring-aop-4.1.5.RELEASE

spring-beans-4.1.5.RELEASE

spring-context-4.1.5.RELEASE

spring-core-4.1.5.RELEASE

spring-expression-4.1.5.RELEASE

spring-web-4.1.5.RELEASE

spring-webmvc-4.1.5.RELEASE

日志包

commons-logging-1.2

关于包的下载:

http://javacui.com/opensource/314.html 

http://javacui.com/opensource/313.html 


在web.xml中配置SpringServlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


定义MVC控制文件spring-mvc.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	<!-- 自动扫描且只扫描@Controller -->
	<context:component-scan base-package="com.test" />
	<!-- 定义JSP文件的位置 --> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>


定制请求控制类,使用注解配置请求地址:

package com.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
	@RequestMapping("/hellow")
	public String hellow(){
		System.out.println("收到请求");
		return "success";
	}
}

这里用到了两个注解:@Controller 和 @RequestMapping。


编写返回展示页面JSP,spring-mvc.xml配置了基本路径为跟路径下的jsp,在文件夹jsp里面编写文件,success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Spring Mvc</title>
</head>
<body>
hello word
</body>
</html>


在首页点击后跳转到这个展示页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>Spring 测试</title>
  </head>
  <body>
  <a href="<%=basePath %>hellow">hellow</a>
  </body>
</html>


部署后看效果。


推荐您阅读更多有关于“ jar spring 入门 mvc Hellowrold SpringServlet ”的文章

上一篇:Apache的ProxyPass指令详解 下一篇:java中HashMap学习

猜你喜欢

发表评论: