100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > springMVC之自定义视图

springMVC之自定义视图

时间:2019-11-17 17:47:28

相关推荐

springMVC之自定义视图

独角兽企业重金招聘Python工程师标准>>>

1.在com.f145a.springmvc.views下新建HelloView.java

package com.f145a.springmvc.views;import java.util.Date;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.ponent;import org.springframework.web.servlet.View;@Componentpublic class HelloView implements View{@Overridepublic String getContentType() {return "text/html";}@Overridepublic void render(Map<String, ?> arg0, HttpServletRequest arg1, HttpServletResponse arg2) throws Exception {arg2.getWriter().println("Hello View,time:"+new Date());}}

2.配置视图BeanNameViewResolver解析器spring.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:context="/schema/context"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd/schema/context /schema/context/spring-context-4.3.xsd/schema/mvc /schema/mvc/spring-mvc-4.3.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.f145a.springmvc"></context:component-scan><!-- 配置视图解析器 :如何把handler方法返回值解析为实际的物理视图 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前端 --><property name="prefix" value="/WEB-INF/views/"></property><!-- 后缀 --><property name="suffix" value=".jsp"></property></bean><!-- 配置视图BeanNameViewResolver解析器:使用视图的 名字来解析视图 --><!-- 通过order属性来定义视图解析器的优先级,order值越小优先级越高 --><bean class="org.springframework.web.servlet.view.BeanNameViewResolver"><property name="order" value="100"></property></bean><!-- 配置国际化资源文件 --><bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename" value="i18n"></property></bean><!-- 配置直接转发的页面 --><!-- 可以直接响应转发的页面,而无需再经过Handler的方法 --><mvc:view-controller path="/success" view-name="success" /><!-- 在实际开发中通常都需要配置mvc:annotation-driveb标签 --><mvc:annotation-driven></mvc:annotation-driven></beans>

3.SpringMVC.java

package com.f145a.springmvc.handlers;import java.util.Date;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {private static final String SUCCESS="success";@RequestMapping("/testView")public String testView(){System.out.println("testView");return "helloView";}}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><a href="springmvc/testView">testView</a></body></html>

欢迎访问我的个人博客http://www.chengzequn.top

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。