閒着無聊配置一遍SSM以及添加功能增刪改查,以下圖,其中坎坷也挺多!!!javascript
1.工程以下圖。(請忽略紅叉,沒有錯誤)css
2.首先配置pom.xml文件。html
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.summer</groupId>
<artifactId>ssmdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>java
<dependencies>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>mysql
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>jquery
<!-- spring aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>git
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>github
<!-- mybatis與spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 數據庫鏈接池 和驅動-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>web
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>spring
<!-- jstl junit servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring單元測試 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- 引入Mybatis分頁插件 -->
<!-- 引入5.0版本在測試的時候會報錯 4.2版本也會報錯
4.2版本老是出現String沒法轉換爲某個對象的錯誤
-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</project>
3.實體類:
package cn.com.model;
/**
*
* @author L
*
*/
public class User {
//主鍵
private int id;
//用戶名
private String username;
//年齡
private String age;
public User() {
super();
}
public User(int id, String username, String age) {
super();
this.id = id;
this.username = username;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
4.建立DAO接口(Mybaits中Mapper就是dao)
5.實現DAO接口(我這裏就是UserMapper.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:必須與對應的接口全類名一致 UserMapper.java
id :必須與對應接口的某個對應的方法名一致即必需要和UserMapper.java接口中的方法同名。
-->
<mapper namespace="cn.com.dao.UserMapper">
<!-- 新增 -->
<insert id="saveUser" parameterType="User" >
insert into t_user(user_name,user_age) values (#{username},#{age})
</insert>
<!-- 修改 -->
<update id="updateUser" parameterType="User" >
update t_user set user_name=#{username},user_age=#{age} where user_id=#{id}
</update>
<!-- 刪除 -->
<delete id="deleteUser" parameterType="int">
delete from t_user where user_id=#{id}
</delete>
<!-- 根據id查找單個用戶 -->
<select id="findUserById" parameterType="int" resultType="User">
select user_id id,user_name username,user_age age from t_user where user_id=#{id}
</select>
<!-- 查詢全部 -->
<select id="findAll" resultType="User">
select user_id id,user_name userName,user_age age from t_user
</select>
</mapper>
6.配置applicationContext.xml(也有叫spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 註解掃描包 -->
<context:component-scan base-package="cn.com" />
<!-- 加載jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--第一步: 配置數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置和mybatis的整合 -->
<!--第二步: 建立sqlSessionFactory工廠 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定Mybatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:sqlConfig.xml"></property>
</bean>
<!-- 配置mybatis的代理接口開發 * 接口類名和映射文件必須同名 * 接口類和映射文件必須在同一個目 錄下 * 接口的映射文件的namespace名稱必須是接口的全限定名
* 接口的方法名必須和映射的statement的id一致 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描全部dao接口的實現,加入到ioc容器中 -->
<property name="basePackage" value="cn.com.dao"></property>
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--第三步: 事物 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置攔截service -->
<aop:config>
<aop:pointcut expression="execution(* cn.com.service..*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事物加強,事物如何切入-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 全部方法都是事物方法 -->
<tx:method name="*"/>
<!-- 以get開始的全部方法 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
7.配置Mybaits配置文件(我寫的是 sqlConfig.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 別名設置 -->
<typeAliases>
<package name="cn.com.model" />
</typeAliases>
<!-- mybatis分頁插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="false"/>
<property name="rowBoundsWithCount" value="false"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/
</plugin>
</plugins>
</configuration>
8.配置log4.properties
### set log levels ###
log4j.rootLogger = INFO , console , debug , error
### console ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### log file ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = ../logs/springmvc-demo.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = INFO
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### exception ###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = ../logs/springmvc-demo_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
###\u9700\u8981\u58F0\u660E\uFF0C\u7136\u540E\u4E0B\u65B9\u624D\u53EF\u4EE5\u4F7Fdruid sql\u8F93\u51FA\uFF0C\u5426\u5219\u4F1A\u629B\u51FAlog4j.error.key not found
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %l %c%n%p: %m%n
### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout
9.配置jdbc.properties(數據鏈接地址/帳戶/密碼/數據庫名稱請自行更改)
#jdbc.properties
jdbc.url = jdbc:mysql://127.0.1.100:3306/test?characterEncoding=utf-8
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root
jdbc.password= root
10.配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 掃描註解包 -->
<context:component-scan base-package="cn.com.dao"/>
<context:component-scan base-package="cn.com.controller"/>
<context:component-scan base-package="cn.com.service"/>
<!-- 配置sprigmvc視圖解析器:解析邏輯試圖
後臺返回邏輯試圖:index
視圖解析器解析出真正物理視圖:前綴+邏輯試圖+後綴====/WEB-INF/page/index.jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--1: mvc:annotation-driven默認建立了 RequestMappingHandlerMapping和 RequestMappingHandlerAdapter對象
提供對JSon數據格式的支持-->
<mvc:annotation-driven />
<!-- 2.配置靜態資源訪問 -->
<mvc:default-servlet-handler/>
</beans>
11.配置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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssmdemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加載Spring容器配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 攔截全部請求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符編碼過濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
12.service類
13.seviceImpl實現
package cn.com.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dao.UserMapper;
import cn.com.model.User;
import cn.com.service.UserService;
/**
*
* @author L
*
*/
@Service
public class UserServiceImpl implements UserService {
/**
* 注入UserMapper接口
*/
@Autowired
private UserMapper userMapper;
/**
* 新增用戶
*/
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}
/**
* 更新用戶
*/
@Override
public boolean updateUser(User user) {
return userMapper.updateUser(user);
}
/**
* 根據Id刪除用戶
*/
@Override
public boolean deleteUser(int id) {
return userMapper.deleteUser(id);
}
/**
* 根據id查找用戶
*/
@Override
public User findUserById(int id) {
User user = userMapper.findUserById(id);
return user;
}
/**
* 查詢全部用戶
*/
@Override
public List<User> findAll() {
List<User> allUser = userMapper.findAll();
return allUser;
}
}
14.Controller類
package cn.com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.com.model.User;
import cn.com.service.UserService;
@Controller
@RequestMapping("/user")
/**
*
* @author L
*
*/
public class UserController {
@Autowired
private UserService userService;
/**
*跳轉到添加用戶界面
*/
@RequestMapping("toAddUser")
public String toAddUser(){
return "addUser";
}
/**
* 添加用戶並重定向
* @param model
* @param user
* @return
*/
@RequestMapping("addUser")
public String addUser(Model model,User user){
if(user != null){
userService.saveUser(user);
}
return "redirect:/user/userInfo";
}
/**
* 修改用戶
* @param model
* @param request
* @param user
* @return
*/
@RequestMapping("updateUser")
public String UpdateUser(Model model,User user){
if(userService.updateUser(user)){
user = userService.findUserById(user.getId());
model.addAttribute("user", user);
return "redirect:/user/userInfo";
}
return "/error";
}
/**
* 查詢全部用戶
* @param request
* @param model
* @return
*/
@RequestMapping("getAllUser")
public String getAllUser(Model model){
List<User> user = userService.findAll();
model.addAttribute("userList",user);
return "allUser";
}
/**
* 查詢單個用戶
* @param id
* @param request
* @param model
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,Model model){
model.addAttribute("user", userService.findUserById(id));
return "editUser";
}
/**
* 根據id刪除用戶
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public String deleteUser(int id,Model model){
model.addAttribute("user", userService.deleteUser(id));
return "redirect:/user/userInfo";
}
/**
* 分頁查詢用戶信息
* @param pn 默認從第一頁開始 請求參數
* @param model
* @return
*/
@RequestMapping("userInfo")
public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
//從第一條開始 每頁查詢五條數據
PageHelper.startPage(pn, 5);
List<User> users = userService.findAll();
//將用戶信息放入PageInfo對象裏
PageInfo page = new PageInfo(users,5);
model.addAttribute("pageInfo", page);
return "allUser";
}
}
後臺的到這基本完成,下面是前臺部分
15.登陸index.jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<!DOCTYPE htm >
<html>
<head>
<title>首頁</title>
<style type="text/css">
a {
text-decoration: none;
color: #fff;
font-size: 14px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: #5BC0DE;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${path}/user/userInfo">進入用戶管理頁</a>
</h3>
</body>
</html>
16.全部數據頁面allUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<!DOCTYPE HTML>
<html>
<head>
<title>用戶列表</title>
<link
href="${path}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"
rel="stylesheet">
<script type="text/javascript"
src="${path}/static/bootstrap-3.3.7-dist/js/jquery-1.10.1.min.js"></script>
<script type="text/javascript"
src="${path}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- 標題 -->
<div class="row">
<div class="col-md-12">
<h1>用戶管理</h1>
</div>
</div>
<!-- 按鈕 -->
<div class="row">
<div class="col-md-4 col-md-offset-8">
<a class="btn btn-primary" href="${path}/user/toAddUser">新增</a>
</div>
</div>
<!-- 表格 -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
<tr>
<th>id</th>
<th>用戶名</th>
<th>年齡</th>
<th>操做</th>
</tr>
<c:forEach items="${pageInfo.list }" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username }</td>
<td>${user.age }</td>
<!-- <td><a class="edit">編輯</a> <a
class="delete">刪除</a></td> -->
<td>
<a type="button" href="${path}/user/getUser?id=${user.id}" class="btn btn-info btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
編輯</a>
<a type="button" href="${path}/user/delUser?id=${user.id}" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true" ></span>
刪除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<hr style="height:1px;border:none;border-top:1px solid #ccc;" />
<!-- 分頁導航欄 -->
<!-- 分頁信息 -->
<div class="row">
<!-- 分頁文字信息,其中分頁信息都封裝在pageInfo中 -->
<div class="col-md-6">
當前第:${pageInfo.pageNum}頁,總共:${pageInfo.pages}頁,總共:${pageInfo.total}條記錄
</div>
<!-- 分頁條 -->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${path}/user/userInfo?pn=1">首頁</a></li>
<c:if test="${pageInfo.hasPreviousPage }">
<li>
<a href="${path}/user/userInfo?pn=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#">${ page_Num}</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li><a href="${path}/user/userInfo?pn=${ page_Num}">${ page_Num}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageInfo.hasNextPage }">
<li>
<a href="${path}/user/userInfo?pn=${pageInfo.pageNum+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<li><a href="${path}/user/userInfo?pn=${pageInfo.pages}">末頁</a></li>
</ul>
</nav>
</div>
</div>
</div>
</body>
</html>
17.編輯用戶頁面,editUser
<%@ 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>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>編輯用戶</title>
<script type="text/javascript">
function updateUser() {
var form = document.forms[0];
form.action = "<%=basePath %>user/updateUser";
form.method = "post";
form.submit();
}
</script>
</head>
<body>
<h1>添加用戶</h1>
<form action="" name="userForm">
<input type="hidden" name="id" value="${user.id }" /> 姓名:<input
type="text" name="username" value="${user.username }" /> 年齡:<input
type="text" name="age" value="${user.age }" /> <input type="button"
value="編輯" onclick="updateUser()" />
</form>
</body>
</html>
18.添加用戶頁面,addUser
<%@ 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 + "/";
%>
<html>
<title>添加用戶</title>
</head>
<body>
<h1>添加用戶</h1>
<form action="" name="userForm">
姓名:<input type="text" name="username"><br>
年齡:<input type="text" name="age"><br>
<input type="button" value="添加"
onclick="addUser()">
</form>
<script type="text/javascript">
function addUser() {
var form = document.forms[0];
form.action = "<%=basePath %>user/addUser";
form.method = "post";
form.submit();
}
</script>
</body>
</html>
static中的文件下載地址:
https://codeload.github.com/twbs/bootstrap/zip/v3.3.7
注:
若是項目名稱和個人不一樣,那麼xml中掃描註解的包須要改動,不然報錯,顯示找不到類,這屬於直接映射的配置