Java Spring框架 + maven+Beans 搭建一个HelloWorld

来源:互联网 发布:金针软件 编辑:程序博客网 时间:2024/06/08 15:56

新建一个Maven工程:

New->Maven Projection

这里写图片描述
目录结构:
这里写图片描述

打开pom.xml Dependencise一栏添加几个包:

这里写图片描述
这里写图片描述
这里写图片描述

打开web.xml加入以下代码:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">    <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

两个<servlet-name>spring<servlet-name>保持一致的名字
<url-pattern>/</url-pattern>表示在WEB-INF目录下寻找一个servlet-name -servet.xml的文件 spring-servlet.xml

在WEB-INF目录下创建 sping-servlet.xml,并加入以下代码:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd          http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- 配置扫描的包 -->    <context:component-scan base-package="com.springdemo.*" />    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->    <mvc:annotation-driven />    <!-- 访问静态资源 -->    <mvc:default-servlet-handler />    <!-- 视图解析器 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/view/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>

在view目录下创建一个 Demo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>首页</title></head><body>    <h1>This is Spring Demo</h1>    <h1>myname is ${myname }</h1></body></html>

这里写图片描述

创建DemoController.java文件

package com.springdemo.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class DemoController {    @RequestMapping("/index")    public String index(Model model) {        model.addAttribute("myname", "txp");        return "Demo";    }}

右键 As Run ->Run on server

阅读全文
0 0
原创粉丝点击