经常工作中的开发,都是配置好后的项目,很少有从头来的情况,给别人配置的时候,发现不能一次成功,有些小问题需要注意,下面是开始可以通过url地址访问controller层中的requestMapping映射地址,并执行controller中的代码,由于篇幅问题,后续会一步步加深,此为最简单的实例创建部分。
大概说下,这个是通过在浏览器的地址栏中输入请求地址,去映射匹配controller中的requestMapping,找到对应的方法,然后执行方法,会在返回处return指定返回的JSP页面,启动时。
匹配映射controller中的地址,这个操作是在配置文件web.xml中配置的,里面DispatcherServlet是控制器,会根据前端的请求进行映射,有servlet-mapping会进行匹配什么样的地址,/是可以映射所有的地址,此处也可以设置其他形式的请求(比如*.do等),这样在浏览器地址栏中输入的地址,要以设置的要求来请求,比如,设置为*.do为地址映射条件,则浏览器中输入的地址要以.do结尾。在设置控制器时,可以在spring-mvc.xml文件中设置,作为参数设置到Dispatcherservlet中。
controller层中的代码执行完毕后,会根据return后面的字符串查找对应的JSP页面,这个是根据在spring-mvc.xml文件中的试图解析器进行设置处理的,里面的内容可以根据自己的情况进行更改。
好了,大概情况就是上面这些,涉及的不深,下面是代码部分,可以根据代码跑起来再说
web.xml文件的内容:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc.xml文件内容;需要注意的是,在value="/WEB-INF/views/中,一定要在WEB-INF的前面加"/",否则会在请求返回的时候,在返回地址中会加上user//WEB-INF/views/showChat.jsp,然后报错,
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.*"/> <mvc:annotation-driven/> <!--3.配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
以上是配置文件的内容,下面是控制层代码部分,因为仅需要跑起来,所以代码比较简洁。
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/login")
public String LoginCheck() {
System.out.println("进入controller层中的方法LoginCheck()");
String result ="showChat";
return result;
}
}
下面是控制层返回的界面文件showChat.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>控制层跳转界面</h1>
</body>
</html>
以上就是所有的代码部分了,操作及运行结果如下所示:
打开浏览器,再地址栏中输入:http://localhost:8080/user/login,回车
运行结果如下所示:
控制台结果: