1.Maven創建一個web項目
會拋出異常
解決辦法:
Java Build Path-》Libraries中添加Tomcat
Project Facets中Dynamic Web Module無法修改更高版本
解決辦法:
首先Window-》Show View-》Navigator-》settings-》org.eclipse.wst.common.project.facet.core.xml-》將web版本修改爲3.0
其次將web.xml中的app-web版本修改爲3.0
最後修改Dynamic Web Module爲3.0
2.pom.xml 添加jar包引用
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jike</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
</build>
</project>
3.web.xml
<?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"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
4.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.jike" />
</beans>
5.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="<%=path%>/login.html" method="post">
username: <input type="text" name="username" /><br /> password: <input
type="password" name="password" /><br /> <input type="submit"
value="submit" /> <input type="reset" value="reset" />
</form>
<font color="red">${error}</font>
</body>
</html>
6.LoginController.java
package com.jike.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.jike.service.LoginService;
@Controller
public class LoginController {
@Resource
LoginService service;
@Resource
HttpServletRequest request;
@RequestMapping("loginPage")
public ModelAndView toLoginPage() {
return new ModelAndView("login.jsp");
}
@RequestMapping("login")
public ModelAndView doLogin() {
String surl = "success.jsp";
String eurl = "login.jsp";
String username = request.getParameter("username");
String password = request.getParameter("password");
return service.doLogin(surl,eurl,username,password);
}
}
7.LoginService.java
package com.jike.service;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
@Service
public class LoginService {
public ModelAndView doLogin(String surl, String eurl, String username, String password) {
if (username == null || "".equals(username)) {
return new ModelAndView(eurl, "error", "用戶名不能爲空!");
}
if (password == null || "".equals(password)) {
return new ModelAndView(eurl, "error", "密碼不能爲空!");
}
if (username.equals("admin") && password.equals("123456")) {
return new ModelAndView(surl);
}
{
return new ModelAndView(eurl, "error", "用戶名或密碼錯誤!");
}
}
}