搭建普通 springmvc
1.如图建立相关文件
建立在WEB-INF下比较安全,不能直接访问资源。
2.建立Controller控制器,如图
3.需要导入的jar包
commons-logging-1.1.jar
4.web.xml 核心代码添加
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>MyJsp.jsp</welcome-file> </welcome-file-list> <!-- 以下是核心代码 --> <servlet> <servlet-name>springMVCTemplet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVCTemplet</servlet-name> <url-pattern>*.jpg</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>/Views/*</url-pattern> </servlet-mapping> </web-app>
5.springmvc-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:p="http://www.springframework.org/schema/p" 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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 静态资源映射--> <mvc:annotation-driven /> <mvc:resources mapping="/Images/**" location="/WEB-INF/Images/" /> <mvc:resources mapping="/Css/**" location="/WEB-INF/Css/" /> <mvc:resources mapping="/Script/**" location="/WEB-INF/Script/" /> <!-- 对模型视图名称的解析,在视图之后加上jsp后缀--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Views" /> <property name="suffix" value=".jsp" /> </bean> <!-- 告诉 spring 在 com.Controller 包下应用了 spring 注解 --> <context:component-scan base-package="com.Controller" /> </beans>
6.MyJspController.java Controller文件的编写
package com.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/test") public class MyJspController { public MyJspController(){ System.out.println("create MyJspController"); } @RequestMapping("/login") public ModelAndView index(HttpServletRequest request, HttpServletResponse response){ String name=request.getParameter("name"); System.out.println("页面加载:"+name); ModelAndView view=new ModelAndView("/Login/login"); view.addObject("welcome","this is a url"); return view; } }// end
7.login.jsp 页面的编写
在页面中加上这句话<h3>your welcome param: ${welcome}</h3>
8.显示结果
url:http://localhost:8888/WebXiangMu/Views/test/login?name=hc
问题:
之前有个问题一直在纠结:/WEB-INF/下的文件怎么也访问不到
一直报这个错误:
WARN [http-8888-exec-1] (DispatcherServlet.java:1120) - No mapping found for HTTP request with URI [/WebXiangMu/WEB-INF/Views/MyJsp.jsp] in DispatcherServlet with name 'springMVCTemplet'
查了半天最后是发现@RequestMapping("/login.h") 文件的后缀不能用jsp 这样就会报错,原因未查。