需将所需jar包拷贝到Web-INF下的lib包下,并导入到类路径下。
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">
<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>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 自动扫描组件 -->
<context:component-scan base-package="com.cxl.springmvc.controllers"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/login/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
login.sp
<!-- JSP指令 page声明 -->
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head></head>
<body>
<form action="login.action" method="post">
账号:<input type="text" name="sid" value="s01"/><br/>
密码:<input type="password" name="pwd" value="ok"/><br/>
<input type="submit" value=" 登 录 "/>
</form>
</body>
</html>
LoginController.java
package com.cxl.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController{
@RequestMapping("/login.action")
public String login(String sid,String pwd) throws Exception{
System.out.println("hello");
if(sid.equals("s01")&&pwd.equals("ok"))
return "forward:Item/ItemList.action";
else
return "redirect:login.jsp";
}
}
Item.java
package com.cxl.springmvc.pojo;
import java.util.Date;
public class Item {
private int id;
private String name;
private double price;
private Date createtime;
private String detail;
public Item(int id, String name, double price, Date createtime, String detail) {
super();
this.id = id;
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
ItemController.java
package com.cxl.springmvc.controllers;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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;
import com.cxl.springmvc.pojo.Item;
@Controller
public class ItemController {
@RequestMapping("/Item/ItemList.action")
public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception{
List<Item> list = new ArrayList<Item>();
list.add(new Item(1, "华为", 6899, new Date(), "okk"));
list.add(new Item(2, "华为", 6899, new Date(), "okk"));
list.add(new Item(3, "华为", 6899, new Date(), "okk"));
list.add(new Item(4, "华为", 6899, new Date(), "okk"));
list.add(new Item(5, "华为", 6899, new Date(), "okk"));
list.add(new Item(6, "华为", 6899, new Date(), "okk"));
list.add(new Item(7, "华为", 6899, new Date(), "okk"));
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList",list);
modelAndView.setViewName("itemList");
return modelAndView;
}
}
itemList.jsp
<%@ 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>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
运行结果:
点击登录按钮之后却报错:
解决办法:
在确定不是因为代码的原因报错,那就是项目编译的问题了,可能因为各种各样的原因导致项目编译失败,试了一下project ->clean。使eclipse重新编译(含class文件、jsp文件、ftl文件等等),前提是Java Build Path中配置好了。这样的再重启服务器项目应该就可以运行了。
成功: