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

springMVC_03_怎么做

2016-06-29 11:03 169 查看
1.上手视频

2.官方示例

3.写demo

4.看开元代码

5.项目实践

最简配通

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_3_0.xsd"

    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">

     

    <!-- spring mvc servlet -->

    <servlet>

        <description>spring mvc servlet</description>

        <servlet-name>springMvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <description>spring mvc 配置文件</description>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>springMvc</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

</web-app>

配置文件

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"

    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包下的所有类,使其认为spring mvc的控制器 -->

    <context:component-scan base-package="hlt.controller" />

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->

        <property name="prefix" value="/" />

        <property name="suffix" value=".jsp" />

    </bean>

</beans>

java类

IndexController 

package hlt.controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@Controller

public class IndexController {

    private static final Logger logger = Logger.getLogger(IndexController.class);

    @RequestMapping(value = "/index2", method = RequestMethod.GET)

    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("/login");

        view.addObject("c", "好 ");

        request.setAttribute("a", "hello");

        logger.info("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");

        return view;

    }

}

页面login.jsp

<?xml version="1.0" encoding="UTF-8" ?>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Insert title here</title>

<%
String str = (String)request.getAttribute("a");

 %>

</head>

<body>中国人${c}   <%=str %>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: