1.Eclipse中创建dynamic web project;
2.导入springmvc需要的jar包,可能导多了,没这么多;jar包拷贝到web-inf下面的lib文件加,不要直接引用,中文名等路径会报错,无法正常执行。
3.web.xml配置;(web.xml名称固定,全部小写)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">4.右击新建source folder文件夹名称config,在里面新建springmvc.xml和上面web.xml中名称相同;
<!-- Filter 定义处理中文乱码 -->
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name>SpringMVCThree</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>
<!-- 配置前端控制器,如果请求是以action结尾就走springmvc查找相应处理器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
source folder:存放java源代码的文件夹,当然也包括一些package文件夹,还可以包含其他文件. 项目构建后,source folder里面的java自动编译成class文件到相应的/web-inf/classes文件夹中,其他文件也会移到/web-inf/classes相应的目录下.
<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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 自动扫描包下面的文件,类上带有@Controller的会注册为bean --> <context:component-scan base-package="com.taray.controller"></context:component-scan> <!-- 代替处理器映射器、处理器适配器 ,mvc注解驱动--> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路径的前缀,后缀,那样在controller中设置视图时候不需要再加了 --> <!-- prefix前缀如果 不添加,那么如果在@controller注解下有一个@requestmapping("name")访问时候会出错--> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean></beans>
5.Controller处理器处理请求需要编写的代码:
@Controllerpublic class UserController { //实际访问通过@RequestMapping("/modify")地址,和方法名无关 @RequestMapping("/modify") public String modify(UserVO uservo,String address,Model model){ if(uservo!=null){ //获取对象,springmvc将表单中name名词和UserVO中的字段名称相同的值赋进去,这儿uservo可以得到页面提交的4个值 model.addAttribute("uservo", uservo); System.out.println("输出uservo.address:"+uservo.getAddress()); //在形参中定义string address单个参数,如果页面有name=“address”的标签,这样在这儿就可以取到它的值 System.out.println("输出address:"+address); //输出uservo.address:某某地址 //输出address:某某地址 } //如果返回值定义的String类型,那么modelandview中视图通过return "页面地址";转到新页面 return "/index"; }}6 . index页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js"></script><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>查询商品列表</title></head><body> <script type="text/javascript"> </script> <div> <h2>springmvc测试1</h2> <form action="modify.action" method="post"> <table> <tr> <td>id:<input type="text" value="${uservo.id }" name="id"/></td> </tr> <tr> <td>name:<input type="text" value="${uservo.name }" name="name"/></td> </tr> <tr> <td>age:<input type="text" value="${uservo.age }" name="age"/></td> </tr> <tr> <td>address:<input type="text" value="${uservo.address }" name="address"/></td> </tr> </table> <input type="submit" value="提交"/> </form></div></body></html>
7.UserVO实体类
package com.taray.pojo;public class UserVO { private int id; private String name; private int age; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }