SpringMVC可以基于Annotation去请求方法,这次我们来实现SpringMVC+mysql的验证与页面跳转。
创建web.xml
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
welcome.jsp
这里创建了Spring提供的servlet,按照约定,要在WEB-INF下建立对应名字-servlet来配置SpringMVC的控制器扫描和跳转页面的配置,spring-servlet如下:
分别在根目录下建立登录成功和登录失败页面success.jsp error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
成功
${username}登录成功
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
错误
${username}登录失败
默认的欢迎页面如下welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
欢迎页面
欢迎你
点此请求DispatcherServlet并跳转
可以用超链接转到show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
跳转后的页面
成功进行了跳转
控制器bean:
package micro.action;
import javax.servlet.http.HttpServletRequest;
import micro.service.LoginCheck;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping
public class FirstSpringMVC {
@RequestMapping("/spring")
public ModelAndView test()
{
String str = "this is a SpringMVC instance!";
return new ModelAndView("show","str",str);
}
@RequestMapping("/login")
public ModelAndView check(HttpServletRequest request)
{
String name = request.getParameter("username");
String password = request.getParameter("password");
//调用业务处理LoginCheck
if(LoginCheck.check(name, password))
{
return new ModelAndView("success","username",name);
}
return new ModelAndView("error","username",name);
}
}
业务处理模块LoginCheck验证用户:
package micro.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import micro.dao.Dao;
public class LoginCheck {
public static boolean check(String name,String password)
{
//处理业务逻辑
try
{
Connection conn = Dao.getConnection();
PreparedStatement p = conn.prepareStatement("select * from test where username = ? and password = ?");
p.setString(1, name);
p.setString(2, password);
ResultSet rs = p.executeQuery();
while(rs.next())
{
Dao.close(rs, p, conn);
return true;
}
Dao.close(rs, p, conn);
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库连接出错");
}
return false;
}
}
持久层Dao获得与关闭链接:
package micro.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dao {
//获得数据库连接
public static Connection getConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/micro";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,"root","root");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
System.out.println("数据库驱动加载出错");
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库出错");
}
return conn;
}
//关闭相关通道
public static void close(ResultSet rs,PreparedStatement p,Connection conn)
{
try
{
rs.close();
p.close();
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据关闭出错");
}
}
}
web.xml:
看了此篇文章是不是感觉收获蛮大