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

Java-springMVC框架:springMVC简单搭建一

2017-01-09 10:07 393 查看
一个简单的MVC框架,基本要熟悉以下步骤

1:定义一个Controller类
2:配置如何配置
3:如何定义url地址 进入方法
4:如何将查询的数据放入作用域(拿到request,response对象)
5:如何返回页面,转发/重定向


springmvc的配置:

1: 引入jar包

commons-beanutils-1.8.0.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
commons-pool-1.2.jar
spring-aop-4.1.5.RELEASE.jar
spring-aspects-4.1.5.RELEASE.jar
spring-beans-4.1.5.RELEASE.jar
spring-context-4.1.5.RELEASE.jar
spring-context-support-4.1.5.RELEASE.jar
spring-core-4.1.5.RELEASE.jar
spring-expression-4.1.5.RELEASE.jar
spring-web-4.1.5.RELEASE.jar
spring-webmvc-4.1.5.RELEASE.jar
spring-webmvc-portlet-4.1.5.RELEASE.jar


2:在web.xml定义Springmvc的核心类Servlet类

springmvc的配置和applicationContext.xml是一种父子关系
在web-inf下建立一个叫:"servlet-name"-servlet.xml文件  zpupload-servlet.xml
如果想改变名称:在servlet核心类中加入:
<init-param>
<param-name>namespace</param-name>
<param-value>fylupload</param-value>
</init-param>
web.xml中所有代码如下:

<display-name>zpupload</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 加载和初始化spring应用上下文 application.setAttribute("contextConfigLocation","classpath:applicationContext.xml") -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 这个监听器就是专门去坚挺上面application的作用域的变化,如果发生了改变(setAttribute),那么它就会把spring 应用用上下文applicationContext(WebApplicationContext)初始化放入到appliation的作用域 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springmvc的核心类 -->
<servlet>
<servlet-name>zpupload</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>fylupload</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>zpupload</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<!-- 乱码问题,post请求乱码 -->
<filter>
<filter-name>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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3:配置fylupload.xml

<!-- 扫包 -->
<context:component-scan base-package="com.zp.upload"></context:component-scan>

<!-- 视图渲染 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 制定页面存放的路径 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 文件的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>


4:定义一个类IndexController测试

package com.zp.upload;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/upload")
public class IndexController {

@RequestMapping("/test")
public String test() {
System.out.println("进来了嘛??");
return null;
}

// http://localhost:8080/zpupload/upload/index1.html @RequestMapping("/index1")
public String index1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "MM-index1");
map.put("age", "17");
// 把map放入作用域,jsp用EL表达式取值
request.setAttribute("map", map);
// 返回page,路径为视图渲染配种中的
// name="prefix"+@RequestMapping("/upload")+@RequestMapping("/index1")+name="suffix"
// 当我们访问 http://localhost:8080/zpupload/upload/index1.html 他实际找的jsp文件为
// /WEB-INF/pages/upload/index1.jsp
return "index1";
}

@RequestMapping("index2")
public ModelAndView index2() {
ModelAndView modelAndView = new ModelAndView();
HashMap<String, Object> map = new HashMap<>();
map.put("name", "MM-index2-ModelAndView");
map.put("age", "18");
modelAndView.addObject("map", map);// request.setAttribute("map", map);
modelAndView.setViewName("upload/index2");// return "index1";
return modelAndView;
}

// 带参数的get访问
// http://localhost:8080/zpupload/upload/index3.html?username=你好 @RequestMapping("index3")
public String index3(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
System.out.println("username========>" + username);
HashMap<String, Object> map = new HashMap<>();
map.put("name", "MM-index3-Parameter");
map.put("age", "18");
request.setAttribute("map", map);
return "index3";
}

// 重定向:http://localhost:8080/zpupload/upload/index4.html?username=你好
@RequestMapping("index4")
public String index4(HttpServletRequest request) {
String username = request.getParameter("username");
System.out.println("username========>" + username);
HashMap<String, Object> map = new HashMap<>();
map.put("name", "MM-index4-redirect");
map.put("age", "18");
request.setAttribute("map", map);
//		return "redirect:success.html?username=mm";
//		return "redirect:/success.jsp";
return "forward:/success.jsp";
/**
* return "redirect:/success.jsp";时
* 会跳转 http://localhost:8080/zpupload/success.jsp * 访问WebContent下的success.jsp文件
*  return "redirect:success.jsp";  --->  不加/
* 是基于命名空间@RequestMapping("/upload"),会跳转/zpupload/upload/success.jsp
* 重定向(不走视图解析,不会拼接路径)一定是webroot下面的页面或者是requestMapping路径,
* WEB-INF下的文件不允许直接访问,需要借助中间跳转
* return "redirect:/success.html"; 时
* 会跳转 http://localhost:8080/zpupload/upload/success.html * 被@RequestMapping("/success")拦截 访问return "common/success";
* 访问 /WEB-INF/pages/common/success.jsp 文件
*
* return "forward:/success.jsp";
* 转发也会跳过视图解析,不拼接路径,访问WebContent下的success.jsp文件
*/

}

@RequestMapping("/success")
public String success() {
return "common/success";
}
}


代码详见

http://download.csdn.net/detail/u013820054/9732369
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java