1、创建Web项目studentManager,并导入相关Jar包; 2、在web.xml中配置Spring MVC的前端控制器等信息; 3、创建Spring MVC配置文件,并配置组件扫描器和视图解析器; 4、创建com. po.Student类包含属性sno, stname, 成绩列表。 5、编写过滤器解决中文乱码问题(post请求方式) 6、在com. controller创建处理器类,使用复杂POJO完成页面信息的绑定,并在控制台输出学生学号、姓名、各科成绩。 初始页面示例: 代码实现: 代码目录: com.controller.StudentController类: package com.controller;
import com.po.Course;
import com.po.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class StudentController {
@RequestMapping("/showStudent")
public String showStudent(Student student){
String sno = student.getSno();
String sname = student.getStname();
List courses = student.getCourses();
System.out.println("学生:"+sname);
System.out.println("学号:"+sno);
for (int i = 0;i com.po.Course类: 注:实体类中的get和set方法以及toString方法可以通过快捷键“alt+insert”快捷生成 package com.po;
public class Course {
private String cname;
private int cscore;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getCscore() {
return cscore;
}
public void setCscore(int cscore) {
this.cscore = cscore;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
", cscore=" + cscore +
'}';
}
} com.po.Student类: package com.po;
import java.util.List;
public class Student {
private String sno;
private String stname;
private List courses;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getStname() {
return stname;
}
public void setStname(String stname) {
this.stname = stname;
}
public List getCourses() {
return courses;
}
public void setCourses(List courses) {
this.courses = courses;
}
} Spring-mvc.xml配置文件:
js文件见文章开头目录处链接 success.jsp页面: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
此处可写个人信息
web.xml文件:处理中文乱码问题
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
DispatcherServlet
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
student.jsp文件: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
学生成绩信息
pom.xml配置文件:
4.0.0
org.example
studentManager
1.0-SNAPSHOT
war
studentManager Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
org.springframework
spring-context
5.2.8.RELEASE
org.springframework
spring-webmvc
5.2.8.RELEASE
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.0
provided
com.fasterxml.jackson.core
jackson-core
2.9.0
com.fasterxml.jackson.core
jackson-databind
2.9.0
com.fasterxml.jackson.core
jackson-annotations
2.9.0
studentManager
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
org.apache.tomcat.maven
tomcat7-maven-plugin
2.1
utf-8
8080
/studentManager
创作不易,谢谢关注,您的关注是博主更新的动力! |
评论列表(0条)