spring3.0 MVC的简单示例
2015-09-18 23:36
573 查看
1.新建web项目,加入相关jar包。
![](https://img-blog.csdn.net/20150918231901554)
2.首先在web.xml中加入DispatcherServlet
3.在web.xml中DispatcherServlet所写的对应位置上创建springmvc-context.xml
4.创建WebRoot/index.jsp、WebRoot/view/student/add.jsp、WebRoot/view/student/list.jsp
内容分别为
index.jsp:
add.jsp:
list.jsp:
5.在src下加入控制类com.controller.StudentController.java
6.为了解决中文乱码 在web.xml中加入过滤器
7.访问http://127.0.0.1:8080/spring_mvc/index.jsp看效果
基本结构:
![](https://img-blog.csdn.net/20150918233419545)
源码下载地址:http://download.csdn.net/detail/qwcs163/9118911
2.首先在web.xml中加入DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--springmvc必须--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
3.在web.xml中DispatcherServlet所写的对应位置上创建springmvc-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.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-3.0.xsd"> <!-- springmvc 注解驱动 --> <mvc:annotation-driven/> <!-- 扫描器 --> <context:component-scan base-package="com"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/view/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.创建WebRoot/index.jsp、WebRoot/view/student/add.jsp、WebRoot/view/student/list.jsp
内容分别为
index.jsp:
<%@ 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>导航页</title> </head> <body> <a href="student/list.do">一览</a> <br/> <a href="student/add.do">新增</a> <br/> </body> </html>
add.jsp:
<%@ 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>新增用户页</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="student/add.do" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交"/> </form> ${result} </body> </html>
list.jsp:
<%@ 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>一览页</title> </head> <body> 一览页面 </body> </html>
5.在src下加入控制类com.controller.StudentController.java
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/student") public class StudentController { @RequestMapping("/list.do") public String list(){ return "student/list"; } @RequestMapping("/add.do") public String add(String name,String age,Model model){ System.out.println(name+"-"+age); String result = (name==null||"".equals(name))?"":name+"新增成功"; model.addAttribute("result",result); return "student/add"; } }
6.为了解决中文乱码 在web.xml中加入过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 解决中文乱码 (只对post提交有效) --> <filter> <filter-name>Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--springmvc必须--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
7.访问http://127.0.0.1:8080/spring_mvc/index.jsp看效果
基本结构:
源码下载地址:http://download.csdn.net/detail/qwcs163/9118911
相关文章推荐