This code specifically describes how to use the SPRINGMVC framework for database access
The specific code is as follows:
SPRINGMVC configuration File Code
<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 ">
<!--Configuring Controller--
<bean id= "UsersController1" name= "/users.action" class= "Cn.neusoft.controller.UsersController1" ></bean >
<!--projects are typically configured with a scan package-
<context:component-scan base-package= "Cn.neusoft.controller" ></context:component-scan>
<!--the adapter, Mapper, JSON converter used to load annotations in real development
<mvc:annotation-driven></mvc:annotation-driven>
<!--non-annotated mapper and adapter--
<!--Configuring the processor Mapper--
<bean class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean>
<!--Configuring the processor adapter--
<bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" ></bean>
<!--another adapter--
<bean class= "Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" ></bean>
<!--configuration View Resolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<!--Configure JSP page prefixes--
<property name= "prefix" value= "/web-inf/jsp" ></property>
<!--configuration suffix--
<property name= "suffix" value= ". JSP" ></property>
</bean>
</beans>
JSP file code
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >
<title>my JSP ' users.jsp ' starting page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-
<body>
<center>
<table border= "1" >
<tr>
<td> User Number </td>
<td> User name </td>
<td> Password </td>
</tr>
<!--the second line starts to show the information, the information is more, we use the loop--
<!--C:foreach label items are the Var used to receive data to define aliases--
<c:foreach items= "${users}" var= "D" >
<tr> <!--${d.userid}. The following userid is
Userinfo the name of the attribute in the entity class, it must be exactly the same, the case is exactly the same
-
<td><a href= "#" >${d.userid}</a></td>
<td>${d.username}</td>
<td></td>
</tr>
</c:forEach>
</table>
</center>
</body>
Another configuration file code
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<display-name></display-name>
<!--SPRINGMVC Front Controller configuration-
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--springmvc.xml specific configuration, here we just read the configuration file--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!--the first type of *.action is only triggered when access to springmvc/login.action has. Action
Springmvc
Second/URL style that matches the current page restful style
The third */* best not to use the final processing needs to jump to a JSP to represent all the content will go through
Dispatcherservlet re-parsing JSP will error
-
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Method code
Package Cn.neusoft.controller;
Import java.util.ArrayList;
Import java.util.List;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.ModelAndView;
Import Org.springframework.web.servlet.mvc.Controller;
Import Cn.neusoft.pojo.Userinfo;
public class UsersController1 implements Controller {
Public Modelandview HandleRequest (HttpServletRequest request,
HttpServletResponse response) throws Exception {
TODO auto-generated Method Stub
Simulate the information in the database
list<userinfo> list = new arraylist<userinfo> ();
Injecting data into the list
Userinfo user1 = new Userinfo ();
User1.setuserid (1);
User1.setusername ("Neusoft");
User1.setpwd ("123");
Userinfo user2 = new Userinfo ();
User2.setuserid (2);
User2.setusername ("Neusoft");
User2.setpwd ("123");
List.add (user1);
List.add (User2);
Modelandview Mav = new Modelandview ();
Passing the value of list in Modelandview
Equivalent to implementing the Request.setattribute ("Users", list);
Mav.addobject ("Users", list);
Specify a JSP page to display
Mav.setviewname ("/users");
return MAV;
}
}
Another method code
Package Cn.neusoft.pojo;
public class Userinfo {
Private Integer userid;
Private String username;
Private String pwd;
@Override
Public String toString () {
Return "Userinfo [userid=" + userid + ", username=" + username
+ ", pwd=" + pwd + "]";
}
Public Userinfo () {
Super ();
}
Public Userinfo (string username, string pwd) {
Super ();
This.username = Username;
This.pwd = pwd;
}
Public Integer GetUserid () {
return userid;
}
public void Setuserid (Integer userid) {
This.userid = userid;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String getpwd () {
return pwd;
}
public void SetPwd (String pwd) {
This.pwd = pwd;
}
}
Learn SPRINGMVC framework, learn how to access data faster and more easily, improve efficiency
June 2017 Short Semester Training Code Summary-----SPRINGMVC