最近玩了一下Spring MVC,试这写个接口啥的,当然是小白水平。然而令人意外的是遇到了很多坑,坑的不能再坑,其实主要是不太懂哈[smile]。
项目是 Maven + Spring
当然你可以直接用Spring MVC
,只不过用Maven
更好管理jar
包罢了,至于配置参考 使用IntelliJ IDEA开发Spring MVC HelloWorld - 云涛的博客博客频道 - CSDN.NET
安装tomcat
服务器
下载intellij idea
(说明一下,要下载专业版IDEA)
话入正题:
在此之前先看一下工程目录:(如图)
,配置dispatcher-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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="main.controller"/>
<!--<mvc:annotation-driven/>-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<!--ViewResolver 视图解析器-->
<!--用于支持Servlet、JSP视图解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<!--如果支持html,html和jsp不可同时支持-->
<!--<property name="suffix" value=".html"/>-->
</bean>
</beans>
**4,配置完这些之后,你就可以写个Controller了,比如我写的是Home
@Controller
public class Home {
@RequestMapping(value = "list",method = RequestMethod.GET)
public @ResponseBody
ArrayList<String> list(){
ArrayList<String> array = new ArrayList<String>();
array.add("test");
array.add("test2");
return array;
}
@RequestMapping(value = "/user",method = RequestMethod.GET)
public @ResponseBody People user(){
People p = new People();
p.setAge("16");
p.setName("lily");
return p;
}
}
打开Safari
,输入localhost:8080/list
;你就会看到你要返回的数据了.
其实到这一步已经差不多完了,如果你只是想写个返回数据的Demo
,
如果你想更进一步,想直接利用ModelAndView
,生成一个页面的话,并且能够接受ModelAndView
传过去的值.
Controller
里面代码如下:
@RequestMapping(value="/userself")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView("/myName");
modelAndView.addObject("name", "Silicn的测试页面");
modelAndView.addObject("nihao","传值:");
return modelAndView;
}
** 坑来了**
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no,">
<title>${name}</title>
</head>
<body>
<h1 style="text-align: center; font-size: 25px; display: block" >
${nihao}随便写个标题吧
</h1>
<div>
<p style="text-indent: 2em">
一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,
一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,一个段落,
</p>
</div>
<div>
<a href="myName.jsp">一个按钮</a>
</div>
</body>
</html>
你会写这样的一个jsp
文件,等你打开uri
之后.你发现,咦,这个EL语法${name}
怎么直接显示了呢?
这就涉及EL
语法问题, 可能你的Safari
默认EL
是关闭的!!
开启如下:
<%@ page isELIgnored="false" %>
然后就没有然后了.
写了这么多,希望大家配置的时候能够避免这写问题.至于怎么配置tomcat
,配置WebApp
,我就不多说了,网上很多!
最后,如果你遇到路径找不到
,无法解析JSON
等问题,你就看看你的jar
包,是否完整, 或者直接复制我的jar
包就可以了!!
有问题,欢迎留言!!一起探讨!!