JAVA框架SSM之Spring-MVC

SSM组合框架之Spring-MVC项目创建示例,帮助新手了解搭建过程

相关Jar包下载

1.创建web项目

2.下载相关jar包并导入

3.创建springmvc配置文件spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:websocket="http://www.springframework.org/schema/websocket"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
<!-- 启用注解 -->
 <context:annotation-config></context:annotation-config>
<!-- 设置使用注解的包 -->
<context:component-scan base-package="com.lesen.service"></context:component-scan>
<!-- 完成请求个注解pojo的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<!-- 对页面路径解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp"></bean>

</beans>

 

 

4.把spring-mvc.xml配置到web.xml中

 <!--该段字节放进去就行 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->
<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>

 

 

5.创建测试类

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestReturn {

@RequestMapping("/hwd")
public String testa(Model mode)
{
// try {
//// PrintWriter out =resp.getWriter();
// 
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();W
// }
mode.addAttribute("how", "HELLO WORD");
return "Login";
}
}

6.创建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>${how}</title>
</head>
<body>
<div> 
</form>
<p>${how}</p>

 

–打开浏览器输入链接出现如图页面则是成功:

http://localhost:8080/pwd/hwd

 

 

 

 

 

 

 

 

上一篇