SpringMVC的简单使用
- 创建maven项目(不使用webapp模板的情况,使用则忽略)
pom中加<packaging>war</packaging> 在src/main下面建个文件夹webapp 并添加为web会自动生成/WEB-INF/和web.xml文件 修改webapp路径,和建webapp文件夹路径一样
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
- 在resources下创建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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ljw.controller"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 在WEB-INF下面创建views包、hello.jsp等jsp文件
(WEB-INF下的jsp文件必须通过控制器转发才能访问到,直接输入url访问不到)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
@Controller
@RequestMapping(value = "/test01")
public class MyController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String HelloCon(){
return "hello";
}
@RequestMapping(value = {"hi","hihi"}, params = {"username","password"})
public String hi(String username, String password){
System.out.println("username:"+username+",password:"+password);
return "hi";
}
@RequestMapping("h/{name}/{pwd}")
public String h(@PathVariable("name") String username, @PathVariable("pwd") String password){
System.out.println("username:"+username+",password:"+password);
return "redirect:/test02/hello02";
}
@Controller
@RequestMapping("/test02")
public class MyController2 {
@RequestMapping("/hello02")
public String helloCon(){
return "hello02";
}
}
hello.jsp文件
<html>
<head>
<title></title>
</head>
<body>
<p>hello</p>
<a href="${pageContext.request.contextPath}/test02/hello02">访问hello02</a>
</body>
</html>
hello02.jsp
<html>
<head>
<title></title>
</head>
<body>
<p>hello2</p>
<a href="${pageContext.request.contextPath}/test01/hi">访问hi</a>
</body>
</html>
hi.jsp
<html>
<head>
<title></title>
</head>
<body>
<p>hi</p>
<a href="${pageContext.request.contextPath}/test01/hello">访问hello</a>
</body>
</html>
地址跳转问题
从test01/hello------>test02/hello02的话
需要从项目根路径开始设置pageContext.request.contextPath取到项目的根路径 避免频繁进出路径
<body>
<p>hello</p>
<a href="${pageContext.request.contextPath}/test02/hello02">访问hello02</a>
</body>
|