SpringMVC helloworld项目搭建
MVC框架做了哪些内容:
a) 将url 映射到java 类处理java 类的方法
b) 封装用户提交的数据
c) 处理请求相关的业务处理--封装响应的数据
d)将响应的数据进行渲染 jsp ,html 等
spring mvc 是一个轻量级的,基于请求响应的MVC框架
基于事件驱动的JSF
hello world springMVC 案例主要搭建步骤:
新建一个web 工程
1 、导入相关jar 包,
代码结构:
2. 配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>01springMvc_hello</display-name>
<servlet>
<!--下面如果是springmvc 那么配置文件必须是springmvc-servlet -->
<servlet-name>springmvc</servlet-name>
<!-- 配置分发器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 容器启动时就加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
load-on-startup 配置1 是容器一启动就加载的意思
url-partern 是OCP 原则
3、添加sprigmvc 配置文件,默认是在webINF 下 [dispatcherServlet -Name] -servlet.xml
上面的工程是配置的是springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置handler Adapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置渲染器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 结果视图的前缀 下面意思是web-inf 下面的jsp 文件夹 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 结果视图的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置请求和处理器 -->
<bean name="/hello.do" class="cpcn.payment.HelloController"></bean>
</beans>
HelloController 代码需要自己编写:
package cpcn.payment;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* 版权所有 (c) 2016,小明有限公司
*/
/**
* 类说明:
*
* <pre>
* Modify Information:
* Author Date Description
* ============ =========== ============================
* wangxiaoming 2016-7-12 Create this file
* </pre>
*
*/
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
ModelAndView mv = new ModelAndView();
//封装要显示到视图中的数据
mv.addObject("msg", "hello springmvc");
// 设置视图名称 hello.jsp
mv.setViewName("hello");
return mv;
}
}
hello.jsp:
<!DOCTYPE HTML>
<html>
<head>
<title>显示页面</title>
</head>
<body>
<!-- EL表达式 -->
${msg}
</body>
</html>
查看下运行结果:
地址是:
http://localhost:8080/hello/hello.do
8080 8090 和你配置的tomcat端口有关
来源:https://blog.csdn.net/wangming520liwei/article/details/53232108