热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我的Spring之旅(一):HelloSpring【附github源码】

1、项目介绍利用SpringMVC控制器,返回网页和json.2、新建Maven工程3、配置pom.xml<projectxmlns"http:maven.apache.or

1、项目介绍

利用Spring MVC控制器,返回网页和json.

2、新建Maven工程



3、配置pom.xml

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.nextgame
hellospring
war
0.0.1-SNAPSHOT
hellospring Maven Webapp
http://maven.apache.org

4.0.5.RELEASE



junit
junit
3.8.1
test



org.springframework
spring-core
${spring.version}



org.springframework
spring-web
${spring.version}



org.springframework
spring-webmvc
${spring.version}




hellospring


4、HelloSpring控制器

①导入servlet-api

从Tomcat的lib目录中导入servlet-api.jar

②导入json-lib


(注:①②可以通过pom.xml添加依赖,请无视我手动导入的笨办法- -)

③源码

package com.nextgame.web;
import java.io.IOException;
import net.sf.json.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.*;
@Controller
public class HelloController {
@RequestMapping("/view")
//返回网页 public String hello(@RequestParam(value="hello", required=false, defaultValue="World") String name, Model model) { model.addAttribute("hello", name); return "helloworld"; }@RequestMapping("/json")
//返回json public void json(HttpServletRequest req,HttpServletResponse res) throws IOException {JSONObject obj = new JSONObject();obj.put("msg", "hello spring");obj.put("copyright", "wang hao");res.getWriter().print(obj);  }}

5、网页

在WEB-INF目录下新建views目录,在views目录下新建hello world.jsp
<%@ page language="java" cOntentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>







Hello:${name}




6、配置Spring servlet

①命名规则:xxx-servlet.xml,这里命名为:dispatcher-servlet.xml

②配置

 xmlns:cOntext="http://www.springframework.org/schema/context"
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-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">



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

/WEB-INF/views/


.jsp



7、配置web.xml

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >


Archetype Created Web Application

dispatcher

org.springframework.web.servlet.DispatcherServlet

1



dispatcher
/



contextConfigLocation
/WEB-INF/dispatcher-servlet.xml




org.springframework.web.context.ContextLoaderListener



8、run as server

http://localhost:8080/hellospring/view

(囧:EL表达式请略过。。。)
http://localhost:8080/hellospring/json



9、源码

下载地址:github



推荐阅读
author-avatar
大众化的公爵樱桃rwr_208
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有