警告: No mapping found for HTTP request with URI [/ssm_y/login.action] in DispatcherServlet with name 'springDispatcherServlet'
时间: 2023-12-04 14:29:28 浏览: 161
这个警告提示是由于在Spring框架中,DispatcherServlet没有找到与请求URI [/ssm_y/login.action] 对应的映射。DispatcherServlet是Spring MVC框架中的核心组件,用于处理客户端请求并将其分发给相应的处理器。
要解决这个问题,你可以检查以下几点:
1. 确保你的请求URI与控制器中定义的映射路径匹配。检查请求URI是否拼写正确,包括大小写。
2. 检查你的控制器类上是否使用了@Controller注解,并且确保相关的@RequestMapping注解指定了正确的映射路径。
3. 检查你的Spring配置文件中是否正确配置了DispatcherServlet,并且指定了正确的映射路径。可以参考官方文档或其他可靠资源来确认配置是否正确。
如果以上步骤都没有解决问题,请提供更多的信息,例如你的控制器类和Spring配置文件的代码片段,以便更好地理解和帮助你解决问题。
相关问题
用idea写 用两种方式整合SSM框架 要求:1.以纯注解的方式整合SSM框架
好的,以下是两种方式整合SSM框架的步骤:
## 1. 整合SSM框架方式一(纯注解方式)
### 1.1 创建Maven项目
在IDEA中创建一个Maven项目,选择Web应用程序类型。
### 1.2 导入SSM框架依赖
在pom.xml文件中添加以下依赖:
```xml
<!-- Spring框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<!-- Spring MVC框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- MyBatis框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- MyBatis-Spring框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- JSTL标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
```
### 1.3 配置web.xml文件
在src/main/webapp/WEB-INF目录下创建web.xml文件,并添加以下配置:
```xml
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置SpringMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet映射 -->
<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>
```
### 1.4 配置Spring配置文件
在src/main/resources目录下创建spring-context.xml文件,并添加以下配置:
```xml
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置MyBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.demo.entity"/> <!-- 指定实体类路径 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/> <!-- 指定Mapper映射文件路径 -->
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.demo.mapper"/> <!-- 指定Mapper接口所在的包 -->
</bean>
```
### 1.5 配置SpringMVC配置文件
在src/main/resources目录下创建spring-mvc.xml文件,并添加以下配置:
```xml
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置静态资源映射 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<!-- 配置扫描Controller -->
<context:component-scan base-package="com.example.demo.controller"/>
```
### 1.6 编写Controller
在src/main/java目录下创建com.example.demo.controller包,并在该包下创建UserController.java文件,编写以下代码:
```java
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public String list(Model model) {
List<User> userList = userService.findAll();
model.addAttribute("userList", userList);
return "user/list";
}
@GetMapping("/add")
public String add() {
return "user/add";
}
@PostMapping("/save")
public String save(User user) {
userService.save(user);
return "redirect:/user/list";
}
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "user/edit";
}
@PostMapping("/update")
public String update(User user) {
userService.update(user);
return "redirect:/user/list";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
userService.delete(id);
return "redirect:/user/list";
}
}
```
### 1.7 编写Service
在src/main/java目录下创建com.example.demo.service包,并在该包下创建UserService.java文件,编写以下代码:
```java
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
User findById(Long id);
void save(User user);
void update(User user);
void delete(Long id);
}
```
在com.example.demo.service包下创建UserServiceImpl.java文件,编写以下代码:
```java
package com.example.demo.service.impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User findById(Long id) {
return userMapper.findById(id);
}
@Override
public void save(User user) {
userMapper.save(user);
}
@Override
public void update(User user) {
userMapper.update(user);
}
@Override
public void delete(Long id) {
userMapper.delete(id);
}
}
```
### 1.8 编写Mapper
在src/main/java目录下创建com.example.demo.mapper包,并在该包下创建UserMapper.java文件,编写以下代码:
```java
package com.example.demo.mapper;
import com.example.demo.entity.User;
import java.util.List;
public interface UserMapper {
List<User> findAll();
User findById(Long id);
void save(User user);
void update(User user);
void delete(Long id);
}
```
在com.example.demo.mapper包下创建UserMapper.xml文件,编写以下代码:
```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">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="userMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT * FROM user
</select>
<select id="findById" resultMap="userMap">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="save">
INSERT INTO user(username, password, age) VALUES(#{username}, #{password}, #{age})
</insert>
<update id="update">
UPDATE user SET username = #{username}, password = #{password}, age = #{age} WHERE id = #{id}
</update>
<delete id="delete">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
```
### 1.9 编写JSP页面
在src/main/webapp/WEB-INF/views/user目录下创建list.jsp、add.jsp和edit.jsp文件,编写以下代码:
list.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Age</th>
<th>Actions</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.age}</td>
<td>
<a href="/user/edit/${user.id}">Edit</a>
<a href="/user/delete/${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="/user/add">Add User</a>
</body>
</html>
```
add.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action="/user/save" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="number" name="age"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
```
edit.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="/user/update" method="post">
<input type="hidden" name="id" value="${user.id}">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="${user.username}"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="${user.password}"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="number" name="age" value="${user.age}"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
```
### 1.10 启动应用
在IDEA中启动应用,访问http://localhost:8080/user/list即可查看用户列表。
## 2. 整合SSM框架方式二(Java配置方式)
### 2.1 创建Maven项目
在IDEA中创建一个Maven项目,选择Web应用程序类型。
### 2.2 导入SSM框架依赖
详见方式一。
### 2.3 配置WebInitializer
在src/main/java目录下创建com.example.demo.config包,并在该包下创建WebInitializer.java文件,编写以下代码:
```java
package com.example.demo.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
```
### 2.4 配置WebConfig
在src/main/java目录下创建com.example.demo.config包,并在该包下创建WebConfig.java文件,编写以下代码:
```java
package com.example.demo.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.sql.DataSource;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.demo.controller")
@MapperScan(basePackages = "com.example.demo.mapper")
@PropertySource("classpath:jdbc.properties")
public class WebConfig implements WebMvcConfigurer {
@Autowired
private Environment env;
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.username"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
```
### 2.5 配置jdbc.properties
在src/main/resources目录下创建jdbc.properties文件,编写以下代码:
```
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123456
```
### 2.6 编写Controller
详见方式一。
### 2.7 编写Service
详见方式一。
### 2.8 编写Mapper
详见方式一。
### 2.9 编写JSP页面
详见方式一。
### 2.10 启动应用
详见方式一。
idea 2020版本用ssm框架写一个maven项目实现简单的增删改查详细教程
好的,下面是详细的教程:
1. 环境准备
首先,需要准备好以下环境:
- IDEA 2020版本
- JDK 1.8或以上
- Maven
- Tomcat
2. 创建Maven项目
打开IDEA,选择File -> New -> Project,选择Maven,在左侧选项卡中选择Web Application,点击Next。在下一个页面中输入Group Id和Artifact Id,然后点击Next。
在下一个页面中,输入项目名称和路径,然后点击Finish。IDEA会自动创建一个Maven项目,并且生成一些基本的文件和目录结构。
3. 配置pom.xml
打开项目的pom.xml文件,添加以下依赖:
```xml
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- 数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Servlet API依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP API依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSTL依赖 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
```
其中,${spring.version}、${mybatis.version}、${mybatis-spring.version}、${mysql.version}、${servlet.version}和${jsp.version}都是需要在pom.xml中定义的变量,例如:
```xml
<properties>
<spring.version>5.2.9.RELEASE</spring.version>
<mybatis.version>3.5.6</mybatis.version>
<mybatis-spring.version>2.0.6</mybatis-spring.version>
<mysql.version>8.0.21</mysql.version>
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.3</jsp.version>
<jstl.version>1.2</jstl.version>
</properties>
```
4. 配置web.xml
在src/main/webapp/WEB-INF目录下创建web.xml文件,并添加以下内容:
```xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SSM Maven Webapp</display-name>
<!-- 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:spring/spring-mvc.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>
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- JSP和静态资源映射 -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
```
5. 编写Spring配置文件
在src/main/resources目录下创建spring目录,并在其中创建spring-context.xml和spring-mvc.xml两个配置文件。
spring-context.xml:
```xml
<!-- 数据源配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.demo.entity"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- Mapper扫描配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.demo.dao"/>
</bean>
```
spring-mvc.xml:
```xml
<!-- 扫描Controller -->
<context:component-scan base-package="com.example.demo.controller"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置静态资源访问 -->
<mvc:default-servlet-handler/>
<!-- 配置SpringMVC注解支持 -->
<mvc:annotation-driven/>
```
6. 编写实体类和Mapper接口
在src/main/java目录下创建com.example.demo.entity和com.example.demo.dao两个包。
在com.example.demo.entity包中创建一个User类,用于表示用户信息:
```java
public class User {
private Integer id;
private String name;
private Integer age;
// 省略getter和setter方法
}
```
在com.example.demo.dao包中创建一个UserMapper接口,用于定义对User表的增删改查操作:
```java
public interface UserMapper {
List<User> listAll();
User getById(Integer id);
void add(User user);
void update(User user);
void delete(Integer id);
}
```
7. 编写Mapper映射文件
在src/main/resources目录下创建mapper目录,并在其中创建UserMapper.xml文件,用于实现UserMapper接口中定义的操作:
```xml
<mapper namespace="com.example.demo.dao.UserMapper">
<select id="listAll" resultType="com.example.demo.entity.User">
select * from user
</select>
<select id="getById" parameterType="java.lang.Integer" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
<insert id="add" parameterType="com.example.demo.entity.User">
insert into user(name, age) values(#{name}, #{age})
</insert>
<update id="update" parameterType="com.example.demo.entity.User">
update user set name = #{name}, age = #{age} where id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
</mapper>
```
8. 编写Controller
在com.example.demo.controller包中创建一个UserController类,用于处理与用户相关的请求:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/list")
public String list(Model model) {
List<User> userList = userMapper.listAll();
model.addAttribute("userList", userList);
return "user/list";
}
@RequestMapping("/add")
public String add() {
return "user/add";
}
@RequestMapping("/doAdd")
public String doAdd(User user) {
userMapper.add(user);
return "redirect:/user/list";
}
@RequestMapping("/edit")
public String edit(Integer id, Model model) {
User user = userMapper.getById(id);
model.addAttribute("user", user);
return "user/edit";
}
@RequestMapping("/doEdit")
public String doEdit(User user) {
userMapper.update(user);
return "redirect:/user/list";
}
@RequestMapping("/delete")
public String delete(Integer id) {
userMapper.delete(id);
return "redirect:/user/list";
}
}
```
9. 编写JSP页面
在src/main/webapp/WEB-INF/views/user目录下创建list.jsp、add.jsp和edit.jsp三个文件,分别用于显示用户列表、添加用户和编辑用户:
list.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>操作</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<a href="${pageContext.request.contextPath}/user/edit?id=${user.id}">编辑</a>
<a href="${pageContext.request.contextPath}/user/delete?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br/>
<a href="${pageContext.request.contextPath}/user/add">添加用户</a>
</body>
</html>
```
add.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action="${pageContext.request.contextPath}/user/doAdd" method="post">
Name: <input type="text" name="name"/><br/>
Age: <input type="text" name="age"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
```
edit.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="${pageContext.request.contextPath}/user/doEdit" method="post">
<input type="hidden" name="id" value="${user.id}"/>
Name: <input type="text" name="name" value="${user.name}"/><br/>
Age: <input type="text" name="age" value="${user.age}"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
```
10. 部署和运行
在IDEA中点击菜单栏的Run -> Edit Configurations,选择Tomcat Server,点击左侧的“+”按钮,选择Local,并设置Tomcat安装目录和应用访问路径。点击OK后,就可以选择新建的配置并点击Run按钮来启动Tomcat服务器。
启动成功后,打开浏览器,访问http://localhost:8080/SSM_Maven_Webapp/user/list即可看到用户列表页面。
阅读全文