Springmvc的原理:网上很多朋友都写的非常好。这里引用几个
http://jinnianshilongnian.iteye.com/blog/1594806
环境:
Eclipse
Tomcat7
JDK1.7
使用SpringMVC+maven
一、建立maven项目
建立之后目录结构如下:如果缺少那就需要手动建立
二、在web.xml中配置SpringMVC
首先是配置DispatcherServlet,根据系列(1)的Spring MVC响应流程图,可以看出DispatcherServlet主要就是拦截请求,然后调用对应的Controller和Action,解析和渲染指定的视图并返回响应
<web-app>
<!-springmvc的配置,入口就是一个servlet -->
<servlet>
<servlet-name>testMavenTomcat</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testMavenTomcat</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-spring的配置,入口就是一个监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、test-servlet.xml,配置springMVC的具体内容
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- spring mvc-->
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/>
<!-- 扫描web包,应用Spring的注解 @Controller -->
<context:component-scan base-package="com.sanshi.controller" />
<!-- 静态资源映射 -->
<mvc:resources mapping="/js/**"location="/WEB-INF/js/" />
<mvc:resources mapping="/css/**"location="/WEB-INF/css/" />
<mvc:resources mapping="/fonts/**"location="/WEB-INF/fonts/" />
<mvc:resources mapping="/plugins/**"location="/WEB-INF/plugins/" />
<mvc:resources mapping="images/**"location="/WEB-INF/images/" />
<!-- view resolver 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<mvc:annotation-driven />
<!-- MVC AnnotationDriven -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="utf8StringHttpMessageConverter"class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- <beanid="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
解决 HttpMediaTypeNotAcceptableException: Could not findacceptable representation
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean> -->
</list>
</property>
</bean>
</beans>
四、建立Controller,具体如下,有多种和前端交互。把数据存放在ModelAndView对象中,或者存放在request域中,
@Controller
@RequestMapping(value="/")
publicclass MvcController {
@Autowired
private HttpServletRequest request;
@RequestMapping("index")
public String index(){
return"/index";
}
@RequestMapping("login")
public ModelAndView login(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username", "Hello World!");
modelAndView.setViewName("business");
returnmodelAndView;
}
@RequestMapping("login2")
public String login2(){
//HttpServletRequestrequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
System.out.println("request="+request);
request.setAttribute("username", "hhhh");
System.out.println("username="+request.getAttribute("username").toString());
return"business";
}
}
这里需要注意一点:获取request对象时,如果自己引用jar包就会冲突,jar包包括
javax.servlet-api,jsp-api
可以直接使用Tomcat的jar,因为我在maven中加入了tomcat插件,所以直接配置如下:
五、前端
index.jsp
<html>
<body>
<%
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
%>
</body>
</html>
login.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>testMavenMVC 例子</title>
<meta charset="utf-8">
<meta name="viewport"content="width=device-width, initial-scale=1.0">
<!--Bootstrap -->
<link rel="stylesheet"href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet"href="http://v3.bootcss.com/examples/signin/signin.css"/>
<!-- HTML5 Shimand Respond.js IE8 support of HTML5 elements and media queries -->
<!--WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if ltIE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<!-- jQuery(necessary for Bootstrap's JavaScript plugins) -->
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<!-- Includeall compiled plugins (below), or include individual files as needed-->
<script src="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form class="form-signin"role="form" action="login">
<h2 class="form-signin-heading">登陆</h2>
<input type="text" name="username"class="form-control" placeholder="请输入用户名" requiredautofocus>
<button class="btn btn-lg btn-primary btn-block" type="submit">登陆</button>
</form>
</body>
</html>
business.jsp 好久不做前端,都忘了很多,这里要备注一下
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
${username} //使用EL表达式,刚开始老是不行,后来再头上加了一行说明就ok了。注意看一下<%@ page isELIgnored="false"%>
</br>
<%=request.getAttribute("username") %> //<%=表达式 %> 不要写分号!
<%
String userName= request.getAttribute("username").toString();
System.out.println("ok ,"+userName);
%> //如果<%%>,内部就是后台数据,记得语句完毕写上分号!
</body>
</html>
六、启动看效果
http://localhost:8088/testMavenTomact/login2?username=admin
最后说明一下maven中添加tomcat插件
<project>
…….
<build>
<finalName>testMavenTomact</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
……
</project>
如果需要https还需要再加-Dmaven.tomcat.httpsPort=8443配置,当然也需要安装安全证书。
如果加上了配置,默认是会报错
结果:C:\Users\Administrator\.keystore (系统找不到指定的文件。)