<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SpringMVC-Annotation</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:ApplicationContext-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
以上是xml文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:annotation-config/> <contet:component-scan base-package="com.Controller"/> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
以上是SpringMVC的xml文件
package com.Controller;import java.util.ArrayList;
import java.util.List;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.Entity.Items;
@Controllerpublic class ItemsController {
@RequestMapping(value="/qureyItems.action")
public ModelAndView qureyItems(){
List<Items> listItems1 = new ArrayList<Items>();
Items items = new Items();
items.setId(1);
items.setChiami("联想");
items.setPrezzo(4400);
listItems1.add(items);
List<Items> listItems2 = new ArrayList<Items>();
Items itemsTwo = new Items();
itemsTwo.setId(2);
itemsTwo.setChiami("戴尔");
itemsTwo.setPrezzo(5500);
listItems2.add(itemsTwo);
List<Items> listItems3 = new ArrayList<Items>();
Items itemsThree = new Items();
iemsThree.setId(3);
itemsThree.setChiami("索尼");
itemsThree.setPrezzo(6000);
listItems3.add(itemsThree);
ModelAndView mv = new ModelAndView();
mv.addObject("listItems1", listItems1);
mv.addObject("listItems2", listItems2);
mv.addObject("listItems3", listItems3);
mv.setViewName("hello");
return mv;
}
}以上是Controller文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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>
<form action="${pageContext.request.contextPath}/qureyItems.action" method="post">
<table width="100%" border=1>
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
</tr>
<c:forEach items="${itemsList }" var="items">
<tr>
<td>${items.id}</td>
<td>${items.chiami}</td>
<td>${items.prezzo}</td>
</c:forEach>
</table>
</form>
</body>这是jsp文件。
最后页面404是下面的样子: