Spring MVC (二)

来源:互联网 发布:lol for mac 国服 编辑:程序博客网 时间:2024/06/07 01:08

简化视图到控制器  

闲话不说 spring确实强大 

jsp页面

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0">     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->  </head>    <body style="font-size:50px;">    HELLO WORLD!    <!-- 当前的时间 --><br/>    当前的时间是;    ${date }<br/>    使用jstl标签:    <c:out value="${date}"></c:out>  </body></html>

web.xml中的配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"  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_2_5.xsd">  <!-- 配置Springmvc --> <!-- spring-Mvc核心控制器 -->  <servlet>  <!-- 其中的servlet-name 可以自己定义  但是如果是自己定义     创建的spring配置文件的名字必须是   (servlet-name)-servlet.xml               系统会自动加载文件名为这个的xml配置文件  否则自己就需要指定文件名称进行加载      -->    <servlet-name>springapp</servlet-name>    <!-- 核心 -->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet><!-- 访问的文件名是以do结尾的当然 也可以是其他的什么比如    *.jsp/*.htm /*.action ...  -->  <servlet-mapping>    <servlet-name>springapp</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>

   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

controller中的配置 HelloAction .java

package com.dragon.controller;

import java.util.Date;

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

import org.springframework.web.servlet.ModelAndView;

public class HelloAction implements  org.springframework.web.servlet.mvc.Controller {        //创建一个控制器类相当于  struts2 中的   action

 //定义变量用来动态的保存需要跳转的页面 private String successView;  public String getSuccessView() {  return successView; }

 public void setSuccessView(String successView) {  this.successView = successView; }

 public ModelAndView handleRequest(HttpServletRequest request,   HttpServletResponse response) throws Exception {  //////////练习1/////////////////////////////  //获得当前的时间信息显示在页面中  Date date = new Date();  //返回                                  视图名称          模型名称       模型的值  return new ModelAndView(this.getSuccessView(), "date", date);  // TODO Auto-generated method stub  //return new ModelAndView("index.jsp"); }

}

springmvc配置文件的配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       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-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet --> <!-- 添加一个bean入口 指定其一个类作为他的入口 --> <bean  name="/helloAction.do" class="com.dragon.controller.HelloAction">  <!-- 其中的name为  控制器类定义的变量successView   value为需要访问的路径 -->  <property name="successView" value="index"></property> </bean> <!-- 为了减少代码量  声明一个视图解决的入口   配置view  -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <!-- 提供jstl支持 -->        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>        <!-- 如果指定了后缀名和前缀名的时候  注入一个参数需要跳转的View地址   就不需要 书写后缀名称  系统则自动匹配-->        <!-- 前缀 -->        <property name="prefix" value="/"></property>        <!-- 后缀   -->        <property name="suffix" value=".jsp"></property>            </bean>

</beans>

项目结构
Spring MVC (二) - 口袋里的小龙 - 口袋里的小龙
 

项目中如果想使用jstl标签  必须引入jstl.jar包

还是一样注释在代码中 不做过多的解释  先学着做再慢慢理解吧!

访问地址http://localhost:8080/Spring_MVC/helloAction.do

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子写字速度慢怎么办 孩子上学不合群怎么办 长期在家带孩子怎么办 孩子写字慢怎么办啊 小孩味口不好怎么办 孩子作业不会教怎么办 幼儿吹空调发烧怎么办 小学生上课不听讲怎么办 孩子成绩下滑家长怎么办 五年级成绩下滑怎么办 婴儿吃纸了怎么办 宝宝吃纸了怎么办 宝宝吃所料怎么办 宝宝爱吃卫生纸怎么办 宝宝看了强光怎么办 宝宝看了浴霸灯怎么办 一周半宝宝腹泻怎么办 小儿三天不大便怎么办 小宝宝腿纹不对怎么办 孩子个子太高怎么办 宝宝拉粑粑干硬怎么办 美国办新生儿护照怎么办 怀孕拉绿色大便怎么办 幼儿园孩子不爱学习怎么办 幼儿园老师管不住孩子怎么办 手机支架夹不住怎么办 画水彩没有留白液怎么办 水彩颜料脱胶了怎么办 指甲油涂到手上怎么办 抖音里没有控雨怎么办 qq视频没有特效怎么办 组长刁难员工员工怎么办 想转行没精力怎么办 桔子树无法退款怎么办 保温箱婴儿哭了怎么办 宝宝不爱吃肉泥怎么办 苹果x卡死了怎么办 苹果6手机用着卡怎么办 二个月宝宝肚子胀气怎么办 一岁半宝宝大便干结怎么办 婴儿上大便费劲怎么办