其他分享
首页 > 其他分享> > 6、spring注解+springMVC注解+Mybatis注解+log4j+idea+maven

6、spring注解+springMVC注解+Mybatis注解+log4j+idea+maven

作者:互联网

1、项目结构如下

 2、编辑pom.xml文件配置依赖

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com</groupId>
  8   <artifactId>ssm_day46</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>ssm_day46 Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18     <maven.compiler.source>1.7</maven.compiler.source>
 19     <maven.compiler.target>1.7</maven.compiler.target>
 20   </properties>
 21 
 22   <dependencies>
 23     <dependency>
 24       <groupId>junit</groupId>
 25       <artifactId>junit</artifactId>
 26       <version>4.12</version>
 27     </dependency>
 28 
 29 
 30     <!--1 https://mvnrepository.com/artifact/org.springframework/spring-beans -->
 31     <dependency>
 32       <groupId>org.springframework</groupId>
 33       <artifactId>spring-beans</artifactId>
 34       <version>4.3.6.RELEASE</version>
 35     </dependency>
 36 
 37 
 38     <!--2 https://mvnrepository.com/artifact/org.springframework/spring-context -->
 39     <dependency>
 40       <groupId>org.springframework</groupId>
 41       <artifactId>spring-context</artifactId>
 42       <version>4.3.6.RELEASE</version>
 43     </dependency>
 44 
 45 
 46     <!-- 3https://mvnrepository.com/artifact/org.springframework/spring-core -->
 47     <dependency>
 48       <groupId>org.springframework</groupId>
 49       <artifactId>spring-core</artifactId>
 50       <version>4.3.6.RELEASE</version>
 51     </dependency>
 52 
 53 
 54     <!--4 https://mvnrepository.com/artifact/org.springframework/spring-expression -->
 55     <dependency>
 56       <groupId>org.springframework</groupId>
 57       <artifactId>spring-expression</artifactId>
 58       <version>4.3.6.RELEASE</version>
 59     </dependency>
 60 
 61 
 62     <!--5 https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 63     <dependency>
 64       <groupId>org.springframework</groupId>
 65       <artifactId>spring-aop</artifactId>
 66       <version>4.3.6.RELEASE</version>
 67     </dependency>
 68 
 69 
 70     <!--6 https://mvnrepository.com/artifact/aopalliance/aopalliance -->
 71     <dependency>
 72       <groupId>aopalliance</groupId>
 73       <artifactId>aopalliance</artifactId>
 74       <version>1.0</version>
 75     </dependency>
 76 
 77 
 78     <!--7 https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
 79     <dependency>
 80       <groupId>org.aspectj</groupId>
 81       <artifactId>aspectjweaver</artifactId>
 82       <version>1.8.10</version>
 83     </dependency>
 84 
 85 
 86     <!--8 https://mvnrepository.com/artifact/log4j/log4j -->
 87     <dependency>
 88       <groupId>log4j</groupId>
 89       <artifactId>log4j</artifactId>
 90       <version>1.2.17</version>
 91     </dependency>
 92 
 93 
 94     <!--9 https://mvnrepository.com/artifact/commons-logging/commons-logging -->
 95     <dependency>
 96       <groupId>commons-logging</groupId>
 97       <artifactId>commons-logging</artifactId>
 98       <version>1.2</version>
 99     </dependency>
100 
101 
102     <!--10 https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
103     <dependency>
104       <groupId>org.springframework</groupId>
105       <artifactId>spring-jdbc</artifactId>
106       <version>4.3.6.RELEASE</version>
107     </dependency>
108 
109 
110     <!--11 https://mvnrepository.com/artifact/org.springframework/spring-orm -->
111     <dependency>
112       <groupId>org.springframework</groupId>
113       <artifactId>spring-orm</artifactId>
114       <version>4.3.6.RELEASE</version>
115     </dependency>
116 
117 
118     <!--12 https://mvnrepository.com/artifact/org.springframework/spring-tx -->
119     <dependency>
120       <groupId>org.springframework</groupId>
121       <artifactId>spring-tx</artifactId>
122       <version>4.3.6.RELEASE</version>
123     </dependency>
124 
125 
126     <!--13 https://mvnrepository.com/artifact/org.springframework/spring-web -->
127     <dependency>
128       <groupId>org.springframework</groupId>
129       <artifactId>spring-web</artifactId>
130       <version>4.3.6.RELEASE</version>
131     </dependency>
132 
133 
134     <!--14 https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
135     <dependency>
136       <groupId>org.springframework</groupId>
137       <artifactId>spring-webmvc</artifactId>
138       <version>4.3.6.RELEASE</version>
139     </dependency>
140 
141 
142     <!--15 https://mvnrepository.com/artifact/org.mybatis/mybatis -->
143     <dependency>
144       <groupId>org.mybatis</groupId>
145       <artifactId>mybatis</artifactId>
146       <version>3.4.6</version>
147     </dependency>
148 
149 
150     <!--16 https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
151     <dependency>
152       <groupId>mysql</groupId>
153       <artifactId>mysql-connector-java</artifactId>
154       <version>5.1.38</version>
155     </dependency>
156 
157 
158     <!--17 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
159     <dependency>
160       <groupId>org.mybatis</groupId>
161       <artifactId>mybatis-spring</artifactId>
162       <version>1.3.2</version>
163     </dependency>
164 
165     <!--18 https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
166     <dependency>
167       <groupId>javax.servlet</groupId>
168       <artifactId>javax.servlet-api</artifactId>
169       <version>3.1.0</version>
170     </dependency>
171 
172     <!--19 https://mvnrepository.com/artifact/javax.servlet/jstl -->
173     <dependency>
174       <groupId>javax.servlet</groupId>
175       <artifactId>jstl</artifactId>
176       <version>1.2</version>
177     </dependency>
178 
179 
180     <!--20 https://mvnrepository.com/artifact/taglibs/standard -->
181     <dependency>
182       <groupId>taglibs</groupId>
183       <artifactId>standard</artifactId>
184       <version>1.1.2</version>
185     </dependency>
186 
187     <!--文件上传使用的jar-->
188     <!--21 https://mvnrepository.com/artifact/commons-io/commons-io -->
189     <dependency>
190       <groupId>commons-io</groupId>
191       <artifactId>commons-io</artifactId>
192       <version>2.4</version>
193     </dependency>
194     <!--22-->
195     <dependency>
196       <groupId>commons-fileupload</groupId>
197       <artifactId>commons-fileupload</artifactId>
198       <version>1.3.3</version>
199     </dependency>
200 
201     <!--23 下载ajax封装数据json格式交互的jar-->
202     <dependency>
203       <groupId>com.fasterxml.jackson.core</groupId>
204       <artifactId>jackson-databind</artifactId>
205       <version>2.9.6</version>
206     </dependency>
207 
208     <dependency>
209       <groupId>com.fasterxml.jackson.core</groupId>
210       <artifactId>jackson-core</artifactId>
211       <version>2.9.6</version>
212     </dependency>
213 
214     <dependency>
215       <groupId>com.fasterxml.jackson.core</groupId>
216       <artifactId>jackson-annotations</artifactId>
217       <version>2.9.6</version>
218     </dependency>
219 
220     <!--使用4的版本否则springmvc的xml-->
221     <dependency>
222       <groupId>com.github.pagehelper</groupId>
223       <artifactId>pagehelper</artifactId>
224       <version>4.1.6</version>
225     </dependency>
226   </dependencies>
227 
228 
229 </project>
pom.xml

3、在java目录下的com.pojo包下创建Scott.java持久化类

 1 package com.pojo;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 import java.io.Serializable;
 6 
 7 /**
 8  * 多方引用一方对象
 9  */
10 @Component
11 public class Scott implements Serializable{
12   //多方所需字段
13   private Integer empno             ;
14   private String  ename           ;
15   private Double  sal         ;
16   private Integer deptno;
17   //一方所需字段
18   private String dname;
19 
20   public Scott() {
21   }
22 
23   public Scott(Integer empno, String ename, Double sal, Integer deptno, String dname) {
24     this.empno = empno;
25     this.ename = ename;
26     this.sal = sal;
27     this.deptno = deptno;
28     this.dname = dname;
29   }
30 
31   public Integer getEmpno() {
32     return empno;
33   }
34 
35   public void setEmpno(Integer empno) {
36     this.empno = empno;
37   }
38 
39   public String getEname() {
40     return ename;
41   }
42 
43   public void setEname(String ename) {
44     this.ename = ename;
45   }
46 
47   public Double getSal() {
48     return sal;
49   }
50 
51   public void setSal(Double sal) {
52     this.sal = sal;
53   }
54 
55   public Integer getDeptno() {
56     return deptno;
57   }
58 
59   public void setDeptno(Integer deptno) {
60     this.deptno = deptno;
61   }
62 
63   public String getDname() {
64     return dname;
65   }
66 
67   public void setDname(String dname) {
68     this.dname = dname;
69   }
70 
71   @Override
72   public String toString() {
73     return "Scott{" +
74             "empno=" + empno +
75             ", ename='" + ename + '\'' +
76             ", sal=" + sal +
77             ", deptno=" + deptno +
78             ", dname='" + dname + '\'' +
79             '}';
80   }
81 }
Scott.java

4、在java目录下的com.mapper包下创建ScottMapper.java映射接口

 1 package com.mapper;
 2 import com.pojo.Scott;
 3 import org.apache.ibatis.annotations.Delete;
 4 import org.apache.ibatis.annotations.Insert;
 5 import org.apache.ibatis.annotations.Select;
 6 import org.apache.ibatis.annotations.Update;
 7 import org.springframework.stereotype.Repository;
 8 
 9 import java.util.List;
10 @Repository
11 public interface ScottMapper {
12     @Select("select * from dept")
13     List<Scott> selectOneList();
14 
15     @Select("select d.dname,e.* from dept d,emp e where e.deptno=d.deptno")
16     List<Scott> selectAll();
17 
18     @Select("select d.dname,e.* from dept d,emp e where e.deptno=d.deptno and e.empno=#{primaryKey}")
19     Scott selectPrimaryKey(Integer primaryKey);
20 
21     @Select("select d.dname,e.* from dept d,emp e where e.deptno=d.deptno and e.deptno=#{foreignKey}")
22     List<Scott> selectForeign(Integer foreignKey);
23 
24     @Insert("insert into emp(empno,ename,sal,deptno) values(#{empno},#{ename},#{sal},#{deptno})")
25     int insertScott(Scott scott);
26 
27     @Update("update  emp set ename=#{ename},sal=#{sal},deptno=#{deptno} where  empno=#{empno}")
28     int updateScott(Scott scott);
29 
30     @Delete("delete from emp where empno=#{primaryKey}")
31     int deleteScott(Integer primaryKey);
32 }
ScottMapper.java

 

5、在java目录下的com.service包下创建业务层接口和实现类

 1 package com.service;
 2 
 3 import com.pojo.Scott;
 4 
 5 import java.util.List;
 6 
 7 public interface ScottService {
 8     List<Scott> downOption();
 9     List<Scott> show();
10     Scott info(Integer primaryKey);
11     List<Scott> optionCondition(Integer foreignKey);
12     int add(Scott emp);
13     int edit(Scott emp);
14     int del(Integer primaryKey);
15 }
ScottService.java
 1 package com.service;
 2 import com.mapper.ScottMapper;
 3 import com.pojo.Scott;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Service;
 6 
 7 import java.util.List;
 8 /*
 9 @Service的功能等同于代理对象在spring的xml文件中动态添加bean节点,
10 如果不指定名称,则底层代理在构建bean节点的时候id是类的第一个首字母小写
11 <bean id="empServiceImpl" class="com.service.EmpServiceImpl"/>
12 @Service(value = "empse")的功能等同于代理对象在spring的xml文件中动态添加id值为empse的bean节点
13 <bean id="empse" class="com.service.EmpServiceImpl"/>
14 */
15 @Service
16 public class ScottServiceImpl implements ScottService {
17     /*
18     @Autowired是spring的资源包下注解,按照对象的类型进行自动注入,
19     和对象名称无关,只要类型是匹配的,代理就会在你需要的时候给你注入对象值
20     @Resource是javax包下的注解,按照对象属性名进行注入,
21     和对象名称关系密切,如果对象名称不匹配,则不能完成注入,会报错
22     总结:相比而言,使用@Resource的出错记录更大,因此我们常规选择使用@Autowired
23      */
24     @Autowired
25     private ScottMapper scottMapper;
26 
27     @Override
28     public List<Scott> downOption() {
29         return scottMapper.selectOneList();
30     }
31 
32     @Override
33     public List<Scott> show() {
34         return scottMapper.selectAll();
35     }
36 
37     @Override
38     public Scott info(Integer primaryKey) {
39         return scottMapper.selectPrimaryKey(primaryKey);
40     }
41 
42     @Override
43     public List<Scott> optionCondition(Integer foreignKey) {
44         return scottMapper.selectForeign(foreignKey);
45     }
46 
47     @Override
48     public int del(Integer primaryKey) {
49         return scottMapper.deleteScott(primaryKey);
50     }
51 
52     @Override
53     public int add(Scott scott) {
54         return scottMapper.insertScott(scott);
55     }
56 
57     @Override
58     public int edit(Scott scott) {
59         return scottMapper.updateScott(scott);
60     }
61 }
ScottServiceImpl.java

6、在java目录下的com.controller包下创建控制类CenterController.java

 1 package com.controller;
 2 import com.pojo.Scott;
 3 import com.service.ScottService;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.ui.Model;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import javax.servlet.http.HttpSession;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 @Controller
15 public class CenterController {
16     @Autowired
17     private ScottService scottService;
18 
19     @RequestMapping("/option.do")
20     public String optionData(HttpSession session){
21         List<Scott> deptlist=scottService.downOption();
22         session.setAttribute("deptlist",deptlist);
23         return "redirect:/main.jsp";
24     }
25 
26     @RequestMapping("/show.do")
27     @ResponseBody
28     public Map<String,Object> showData(Integer foreignKey){
29         Map<String,Object> map=new HashMap<String,Object>();
30         List<Scott> emplist=null;
31         //根据部门编号条件查询
32         if(foreignKey!=-1){
33             emplist=scottService.optionCondition(foreignKey);
34         }else{  //查询所有
35             emplist=scottService.show();
36         }
37         map.put("emplist",emplist);
38         return map;
39     }
40 
41 
42     @RequestMapping("/info.do")
43     public String info(Integer primaryKey, Model model){
44         Scott emp=scottService.info(primaryKey);
45         model.addAttribute("emp2",emp);
46         return "/edit.jsp";
47     }
48 
49 
50     @RequestMapping("/edit.do")
51     public String edit(Scott emp){
52         //根据主键编号查询只有一条数据
53         System.out.println(scottService.edit(emp)>0?"edit success":"edit fail");;
54         //使用重定向跳转
55         return "redirect:/main.jsp";
56     }
57 
58     @RequestMapping("/add.do")
59     public String add(Scott emp){
60         //根据主键编号查询只有一条数据
61         System.out.println(scottService.add(emp)>0?"add success":"add fail");;
62         //使用重定向跳转
63         return "redirect:/main.jsp";
64     }
65 
66     @RequestMapping("/del.do")
67     public String del(Integer primaryKey){
68         //根据主键编号查询只有一条数据
69         System.out.println(scottService.del(primaryKey)>0?"del success":"del fail");;
70         //使用重定向跳转
71         return "redirect:/main.jsp";
72     }
73 }
CenterController.java

7、在resouces目录下创建log4j.properties日志文件

 1 log4j.rootLogger=DEBUG, Console  
 2 #Console  
 3 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 4 log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
 5 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
 6 
 7 log4j.logger.org.apache=INFO  
 8 log4j.logger.java.sql.ResultSet=INFO  
 9 log4j.logger.java.sql.Connection=DEBUG  
10 log4j.logger.java.sql.Statement=DEBUG  
11 log4j.logger.java.sql.PreparedStatement=DEBUG   
log4j.properties

8、在webapp目录下的WEB-INF文件夹下创建springmvc-servlet.xml核心配置文件,和web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:mvc="http://www.springframework.org/schema/mvc"
 8        xsi:schemaLocation="http://www.springframework.org/schema/beans
 9        http://www.springframework.org/schema/beans/spring-beans.xsd
10        http://www.springframework.org/schema/aop
11        http://www.springframework.org/schema/aop/spring-aop.xsd
12        http://www.springframework.org/schema/context
13        http://www.springframework.org/schema/context/spring-context.xsd
14        http://www.springframework.org/schema/tx
15        http://www.springframework.org/schema/tx/spring-tx.xsd
16        http://www.springframework.org/schema/mvc
17        http://www.springframework.org/schema/mvc/spring-mvc.xsd
18 ">
19    <!--1.mvc注解驱动-->
20     <mvc:annotation-driven/>
21     <!--2.全局扫描包-->
22     <context:component-scan base-package="com"/>
23     <!--3.驱动管理数据源-->
24     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
25         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
26         <property name="url" value="jdbc:mysql://localhost:3306/ar"/>
27         <property name="username" value="root"/>
28         <property name="password" value="123456"/>
29     </bean>
30     <!--4.数据源事务管理-->
31     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
32          <property name="dataSource" ref="dataSource"/>
33     </bean>
34     <!--5.事务注解驱动-->
35     <tx:annotation-driven transaction-manager="transactionManager"/>
36     <!--6.sqlsessionfacotrybean-->
37     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
38         <property name="dataSource" ref="dataSource"/>
39     </bean>
40     <!--7.映射扫描参数-->
41     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
42         <property name="basePackage" value="com.mapper"/>
43     </bean>
44 </beans>
springmvc-servlet.xml
 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6    <filter>
 7      <filter-name>Character</filter-name>
 8      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 9      <init-param>
10        <param-name>encoding</param-name>
11        <param-value>UTF-8</param-value>
12      </init-param>
13    </filter>
14   <filter-mapping>
15     <filter-name>Character</filter-name>
16     <url-pattern>/*</url-pattern>
17   </filter-mapping>
18   <servlet>
19     <servlet-name>springmvc</servlet-name>
20     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21   </servlet>
22   <servlet-mapping>
23     <servlet-name>springmvc</servlet-name>
24     <url-pattern>*.do</url-pattern>
25   </servlet-mapping>
26   <welcome-file-list>
27     <welcome-file>option.do</welcome-file>
28   </welcome-file-list>
29 </web-app>
web.xml

10、在webapp目录下创建js目录,且将jquery的库文件jquery-1.8.3.js拷贝进去

11、在webapp目录下创建main.jsp文件

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 <%@ page isELIgnored="false" %>
 4 <html>
 5 <head>
 6     <title>首页</title>
 7     <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
 8     <script type="text/javascript" src="js/show.js"></script>
 9 </head>
10 <body>
11    <a href="add.jsp">添加</a>
12     <p>
13          <span>部门:</span>
14         <select name="foreignKey">
15             <option value="-1">--请选择--</option>
16             <c:forEach var="i" items="${deptlist}">
17                   <option value="${i.deptno}">${i.dname}</option>
18             </c:forEach>
19         </select>
20         <input type="button" value="查询">
21     </p>
22     <table></table>
23 </body>
24 </html>
main.jsp

12、在webapp目录的js目录下创建show.js文件

 1 $(function () {
 2     //初始化查询所有
 3     initData(-1);
 4     //下拉列表条件查
 5     $("[type=button]").click(function () {
 6         var foreignKey=$("[name=foreignKey]").val();
 7         initData(foreignKey)
 8     });
 9 
10 });
11 
12 function initData(foreignKey) {
13    $.ajax({
14        url:"show.do",
15        type:"post",
16        dataType:"json",
17        data:{"foreignKey":foreignKey},
18        async:true,
19        success:function (obj) {
20            //将值打印在控制台
21            console.log(obj);
22            //为了防止频繁发送请求重复拼接问题,因此在拼接数据之前,先清楚原有的数据内容
23            $("table").html(" ");
24            var str=" <tr>";
25               str+="     <th>员工编号</th>";
26               str+="     <th>员工姓名</th>";
27               str+="     <th>员工工资</th>";
28               str+="     <th>部门名称</th>";
29               str+="     <th>操作</th>";
30               str+=" </tr>";
31            for(var i=0;i<obj.emplist.length;i++){
32                str+="<tr>";
33                str+="<td>"+obj.emplist[i].empno     +"</td>";
34                str+="<td>"+obj.emplist[i].ename     +"</td>";
35                str+="<td>"+obj.emplist[i].sal       +"</td>";
36                str+="<td>"+obj.emplist[i].dname+"</td>";
37                str+="<td>" +
38                    "<a href='info.do?primaryKey="+obj.emplist[i].empno+"'>修改</a>" +
39                    "|" +
40                    "<a href='del.do?primaryKey="+obj.emplist[i].empno+"'>删除</a>" +
41                    "</td>";
42                str+="</tr>";
43            }
44            $("table").append(str);
45        },
46        error:function () {
47            alert("get data error");
48        }
49    });
50 }
show.js

13、在webapp目录下创建add.jsp文件

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 <%@ page isELIgnored="false" %>
 4 <html>
 5 <head>
 6     <title>添加信息</title>
 7 </head>
 8 <body>
 9    <fieldset>
10        <legend>添加员工信息</legend>
11        <form action="add.do" method="post">
12            <table>
13                <tr>
14                    <th>部门名称:</th>
15                    <td>
16                        <select name="dept.deptno">
17                            <option value="-1">--请选择--</option>
18                            <c:forEach var="i" items="${deptlist}">
19                                <option value="${i.deptno}">${i.dname}</option>
20                            </c:forEach>
21                        </select>
22                    </td>
23                </tr>
24                <tr>
25                    <th>员工编号:</th>
26                    <td><input type="text" name="empno"/></td>
27                </tr>
28                <tr>
29                    <th>员工姓名:</th>
30                    <td><input type="text" name="ename" /></td>
31                </tr>
32                <tr>
33                    <th>员工薪资:</th>
34                    <td><input type="text" name="sal" /></td>
35                </tr>
36                <tr>
37                    <td colspan="2" align="center">
38                        <input type="submit" value="提交"/>
39                        <input type="reset" value="重置"/>
40                    </td>
41                </tr>
42            </table>
43        </form>
44    </fieldset>
45 </body>
46 </html>
add.jsp

14、在webapp目录下创建edit.jsp文件

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 <%@ page isELIgnored="false" %>
 4 <html>
 5 <head>
 6     <title>编辑信息</title>
 7 </head>
 8 <body>
 9    <fieldset>
10        <legend>编辑员工信息</legend>
11        <form action="edit.do?empno=${emp2.empno}" method="post">
12            <table>
13                <tr>
14                    <th>部门名称:</th>
15                    <td>
16                        <select name="dept.deptno">
17                            <option value="-1">--请选择--</option>
18                            <c:forEach var="i" items="${deptlist}">
19                                <c:choose>
20                                    <c:when test="${emp2.dept.deptno eq i.deptno}">
21                                        <option value="${i.deptno}" selected="selected">${i.dname}</option>
22                                    </c:when>
23                                    <c:otherwise>
24                                        <option value="${i.deptno}">${i.dname}</option>
25                                    </c:otherwise>
26                                </c:choose>
27                            </c:forEach>
28                        </select>
29                    </td>
30                </tr>
31                <tr>
32                    <th>员工姓名:</th>
33                    <td><input type="text" name="ename" value="${emp2.ename}"/></td>
34                </tr>
35                <tr>
36                    <th>员工薪资:</th>
37                    <td><input type="text" name="sal" value="${emp2.sal}"/></td>
38                </tr>
39                <tr>
40                    <td colspan="2" align="center">
41                        <input type="submit" value="提交"/>
42                        <input type="reset" value="重置"/>
43                    </td>
44                </tr>
45            </table>
46        </form>
47    </fieldset>
48 
49 </body>
50 </html>
edit.jsp

15、部署启动运行

 此文章为原创,转载请注明出处!需要本案例源码,理论讲解视频,代码操作视频的,请私信联系作者!

 

标签:java,springMVC,import,spring,Scott,springframework,org,注解,public
来源: https://www.cnblogs.com/holly8/p/16623277.html