在Idea中只有src src没有任何包 WEB-INF且WEB-INF只有classes lib条件下结合EL,利用表单获取人员信息,将人员信息保存在JavaBean中,输出人员信息。以及编写web.xml以及各代码书写位置
时间: 2024-10-24 21:10:12 浏览: 23
java读取WEB-INF或src目录下的properties配置文件
5星 · 资源好评率100%
在IntelliJ IDEA中创建一个简单的Spring MVC应用,你需要按照以下步骤操作:
1. **目录结构**:
- `src/main/java` (Java源码目录)
- `com.example.yourproject` (命名空间,例如`yourproject`代表你的项目名称)
- `controllers` (控制器类所在的包)
- `PersonController.java` (用于处理请求并操作人员信息的控制器)
- `models` (模型层,存放JavaBean)
- `views` (视图层,通常是Thymeleaf或JSP文件)
- `templates` (存放HTML模板)
- `personDetails.html` (显示人员信息的模板)
- `META-INF` (Spring配置文件通常放在这里)
- `spring-servlet.xml` (WebApplicationContext配置文件)
2. **web.xml** (放置在`src/main/webapp/WEB-INF`目录下):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
id="WebApp_ID" version="4.0">
<!-- 指定Spring的Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 将Spring Servlet映射到URL模式 "/" -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 其他配置如监听器、过滤器等 -->
</web-app>
```
3. **PersonController.java**:
```java
package com.example.yourproject.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.yourproject.models.Person;
@Controller
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
@GetMapping("/person")
public String getPersonForm(Model model) {
Person person = new Person();
model.addAttribute("person", person);
return "personDetails";
}
@PostMapping("/person/save")
public String savePerson(@ModelAttribute("person") Person person) {
personService.save(person);
return "redirect:/person"; // 返回成功页面,并刷新
}
}
```
4. **Person.java**:
```java
package com.example.yourproject.models;
public class Person {
private String name;
private int age;
// getters and setters
}
```
5. **personDetails.html**:
```html
<!DOCTYPE html>
<html th:lang="${locale}">
<head>
<!-- ...头部内容... -->
</head>
<body>
<form th:action="@{/person/save}" method="post">
Name: <input type="text" th:field="*{name}"><br/>
Age: <input type="number" th:field="*{age}"><br/>
<input type="submit" value="Save"/>
</form>
</body>
</html>
```
6. **在spring-servlet.xml中添加依赖和配置**:
```xml
<!-- ...其他XML配置... -->
<bean id="personService" class="com.example.yourproject.services.PersonServiceImpl"/>
<mvc:annotation-driven />
<context:component-scan base-package="com.example.yourproject" />
<!-- Thymeleaf的配置 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- ...配置Thymeleaf的资源查找路径... -->
</bean>
<bean class="org.thymeleaf.spring5.springsecurity5.DigestingSpringSecurityViewResolver">
<!-- ...配置Spring Security的Thymeleaf视图解析... -->
</bean>
<!-- ...其他配置... -->
```
阅读全文