SpringMVC入门
新建dynamic web project
导入包:
示例1 :
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_spring_mvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 读取springmvc配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截规则 -->
<!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
<!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
<!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
<!-- <url-pattern>*.sikiedu</url-pattern> -->
<url-pattern>/</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>
servlet-class标签的内容是DispatcherServlet的全包名 ,如下:
新建“springmvc.xml”,并导入约束:
springmvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/Views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
ItemController.java:
/**
*
*/
package com.sikiedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
@Controller
public class ItemController {
@RequestMapping(value="welcome")
public String Test(){
return "helloworld";
}
}
helloworld.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>
helloWorld
</body>
</html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>
<a href="welcome">welcome to springmvc</a>
</body>
</html>
运行结果:
示例2:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_spring_mvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 读取springmvc配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截规则 -->
<!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
<!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
<!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
<!-- <url-pattern>*.sikiedu</url-pattern> -->
<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>
springmvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
ItemController.java:
/**
*
*/
package com.sikiedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
@Controller
public class ItemController {
@RequestMapping(value="list.do")
public ModelAndView Test(){
ModelAndView mav=new ModelAndView();
mav.setViewName("item_list");
return mav;
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>
<a href="list.do">welcome to springmvc</a>
</body>
</html>
运行结果:
示例3:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_spring_mvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 读取springmvc配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截规则 -->
<!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
<!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
<!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
<!-- <url-pattern>*.sikiedu</url-pattern> -->
<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>
springmvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
ItemController.java:
/**
*
*/
package com.sikiedu.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.sikiedu.bean.ItemInfo;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
@Controller
public class ItemController {
@RequestMapping(value={"list.do","mylist.do"},method={RequestMethod.GET,RequestMethod.POST })
public ModelAndView Test(){
ModelAndView mav=new ModelAndView();
ItemInfo info1=new ItemInfo("1", "王者荣耀", "moba", "0");
ItemInfo info2=new ItemInfo("2", "王者荣耀", "moba", "0");
ItemInfo info3=new ItemInfo("3", "王者荣耀", "moba", "0");
ItemInfo info4=new ItemInfo("4", "王者荣耀", "moba", "0");
List<ItemInfo> itemList=new ArrayList<ItemInfo>();
itemList.add(info1);
itemList.add(info2);
itemList.add(info3);
itemList.add(info4);
mav.addObject("itemList", itemList);
mav.setViewName("item_list");
return mav;
}
// 转发
@RequestMapping("forwardString.do")
public String forwardString(){
return "forward:list.do";
}
// 重定向
@RequestMapping("redirectString.do")
public String redirectString(){
return "redirect:/index.jsp";
}
@RequestMapping(value="testList.do")
public String Test1(Model model){
ItemInfo info1=new ItemInfo("1", "王者荣耀", "moba", "0");
ItemInfo info2=new ItemInfo("2", "王者荣耀", "moba", "0");
ItemInfo info3=new ItemInfo("3", "王者荣耀", "moba", "0");
ItemInfo info4=new ItemInfo("4", "王者荣耀", "moba", "0");
List<ItemInfo> itemList=new ArrayList<ItemInfo>();
itemList.add(info1);
itemList.add(info2);
itemList.add(info3);
itemList.add(info4);
model.addAttribute("itemList", itemList);
return "item_list";
}
}
ItemInfo.java:
/**
*
*/
package com.sikiedu.bean;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
public class ItemInfo {
private String item_id;
private String item_name;
private String item_type;
private String item_price;
public ItemInfo(String item_id, String item_name, String item_type, String item_price) {
super();
this.item_id = item_id;
this.item_name = item_name;
this.item_type = item_type;
this.item_price = item_price;
}
public String getItem_id() {
return item_id;
}
public void setItem_id(String item_id) {
this.item_id = item_id;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public String getItem_type() {
return item_type;
}
public void setItem_type(String item_type) {
this.item_type = item_type;
}
public String getItem_price() {
return item_price;
}
public void setItem_price(String item_price) {
this.item_price = item_price;
}
@Override
public String toString() {
return "ItemInfo [item_id=" + item_id + ", item_name=" + item_name + ", item_type=" + item_type
+ ", item_price=" + item_price + "]";
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>
<a href="list.do">welcome to springmvc</a>
<form action="${pageContext.request.contextPath}/list.do" method="get">
<input type="submit" value="提交:list.do">
</form><br>
<form action="${pageContext.request.contextPath}/mylist.do" method="post">
<input type="submit" value="提交:mylist.do">
</form><br>
<form action="${pageContext.request.contextPath}/forwardString.do">
<input type="submit" value="提交:forwardString.do">
</form><br>
<form>
<input type="text" value="url末尾输入:redirectString.do">
</form><br>
<form action="${pageContext.request.contextPath}/testList.do">
<input type="submit" value="提交:testList.do">
</form><br>
</body>
</html>
item_list.jsp:
运行结果: