您的位置:首页 > 编程语言 > Java开发

springMVC(一)、springMVC框架开发环境搭建

2017-03-13 15:44 639 查看

一、项目目录展示



二、主要文件内容展示

1、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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- 将项目中所有的请求(/) 都转发到org.springframework.web.servlet.DispatcherServlet配置中去扫描,解析,执行 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


2、mvc-dispatcher-servlet.xml

<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 负责调用controller层,扫描当前路径中的类,发现在类中使用@Controller注解,类似继承HttpServlet/ActionSupport,可添加多个controller -->
<context:component-scan base-package="com.lattice.controller" />

<mvc:annotation-driven />
<mvc:default-servlet-handler/>

<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/jsp/**" location="/jsp/" />
<mvc:resources mapping="/html/**" location="/html/" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 将jsp页面放到web-inf文件夹下,用户无法直接访问页面,可以起到阻止非法访问的作用。 -->
<!-- 前缀,确定当前访问的页面路径地址 -->
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<!-- 后缀,确定要访问的文件类型 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>


3、TestController.java(com.lattice.controller.TestController)

package com.lattice.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("hello")
public class TestController {
@RequestMapping("getList")
public String getList(){
System.out.println("getList");
return"success";//跳转到success.jsp
}
}



三、springMVC用到的注解说明

1、@Controller

@Controller用来修饰类,springmvc会在被@Controller修饰的类中扫描请求。

2、@requestMapping

A、修饰类
前台提交请求时先对类进行匹配,在去这个类中匹配处理请求的具体方法。

B、修饰类中的方法
在扫描请求时匹配具体方法。

3、@SessionAttributes(value={"hello","studentList"},types={String.class,List.class})

springMVC三大作用域:requestScope,sessionScope,applicationScope,用于在跳转的jsp页面中处理显示从请求中设置到三大作用域的数据。

而@SessionAttributes则用来修饰类,指定绑定到sessionScope作用域的数据的类型以及在存放数据,获取数据时需要使用的别名,简单的说就是在setAttribute和使用EL表达式时的Key。

4、@ModelAttribute与public void updateStudent(@ModelAttribute("peter")StudentEntity student){}

A、@ModelAttribute修饰一个数据格式化的方法,这个方法会在这个类接受请求时先于请求处理方法执行

如:

/*
* 使用@ModelAttribute注解来处理前台表单提交的时候,未在表单中输入的数据,
* 防止在进行数据更改或者插入时,在数据库中插入或者修改数据成空值
*/
@ModelAttribute
//带有modelAtrribute注解的方法会在类对象执行方法的时候先执行
public void getStudent(Map<String,Object> map){
StudentEntity student0=new StudentEntity();
student0.setS_id("12341323");
student0.setS_name("小百");
student0.setS_age(19);
student0.setS_loginName("sdjk");
student0.setS_passWord("12");
student0.setT_id("2");

map.put("student", student0);
//此处key值最好设置成小写开头的同实体类类名形式,如使用其他key,在调用的时候需要作出指定
//map.put("peter", student0);
}
B、public void updateStudent(@ModelAttribute("peter")StudentEntity student){}这样使用@ModelAttribute,对接受表单提交的数据进行格式化。

@RequestMapping("update")
public void updateStudent(@ModelAttribute("peter")StudentEntity student){
//do update
}

5、@RequestParam("lattice")String lattice

@RequestParam("lattice")用于接收参数,并且制定传递过来的参数的key名称.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息