1.项目结构
2. web.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC_01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置springMVC的启动
-->
<servlet>
<!--可以约束springMVC的主配置文件如果在默认路径下时的文件名
-->
<servlet-name>springMVC</servlet-name>
<!--配置前端控制器
-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--当主配置文件不在默认路径下,则需要配置文件的路径
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springMVC-servlet.xml</param-value>
</init-param>
<!--配置springMVC 随程序启动而启动
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--/:表示拦截所有请求
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
3. springMVC-servlet.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 启用注解 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.zhq.controller"/>
<!--视图分解器,把controller控制器返回的视图名称加上前缀及后缀,视图的路径最终得到/视图名.jsp
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4. StudentController.java 注解方式
package com.zhq.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.zhq.entity.Student;
@Controller
@RequestMapping(value= {"/student","/stu"})
public class StudentController {
@RequestMapping("/list")
public ModelAndView list() {
List<Student> list=new ArrayList<Student>();
Student stu=new Student();
stu.setName("张三");
stu.setAge(20);
stu.setSex("男");
stu.setPhone("13125283736");
stu.setAddress("广东广州");
list.add(stu);
return new ModelAndView("studentList","data",list);
}
//method=RequestMethod.GET表示只接收GET请求,一般要初始化数据时调用
@RequestMapping(value="/add",method=RequestMethod.GET)
public ModelAndView add() {
System.out.println("执行GET方式提交");
return new ModelAndView("studentAdd");
}
//method=RequestMethod.POST表示只接收POST请求
@RequestMapping(value="/add",method=RequestMethod.POST)
public ModelAndView add(Student student) {
System.out.println("执行POST方式提交");
return new ModelAndView("studentAdd");
}
//占位符方式传递参数/del/100
@RequestMapping("/del/{id:\d+}")//\d+表示只匹配数字
public ModelAndView delete(@PathVariable(value="id") Integer id) {
System.out.println(id);
return new ModelAndView("studentAdd","id",id);
}
//默认传递参数方式/find?id=100
@RequestMapping("/find")
public ModelAndView findStudentByid(Integer id) {
System.out.println(id);
return new ModelAndView("studentAdd","id",id);
}
}
5. 实体类
package com.zhq.entity;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private int studentNo;
private String name;
private String sex;
private int age;
private String phone;
private String address;
private Date birthday;
private String email;
private Grade grade;
public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public String toString() {
return "Student [studentNo=" + studentNo + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone="
+ phone + ", address=" + address + ", birthday=" +
sdf.format(birthday) + ", email=" + email
+ "]";
}
}
package com.zhq.entity;
import java.util.List;
public class Grade {
private int gradeId;
private String gradeName;
private List<Student> students=null;
public int getGradeId() {
return gradeId;
}
public void setGradeId(int gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]";
}
}
package com.zhq.entity;
public class Subject {
private int subjectNo;
private String subjectName;
private int classHour;
private Grade grade;
public int getSubjectNo() {
return subjectNo;
}
public void setSubjectNo(int subjectNo) {
this.subjectNo = subjectNo;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getClassHour() {
return classHour;
}
public void setClassHour(int classHour) {
this.classHour = classHour;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Subject [subjectNo=" + subjectNo + ", subjectName=" + subjectName + ", classHour=" + classHour
+ "]";
}
}
6. 页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/student/list">学生信息列表</a><br>
<a href="${pageContext.request.contextPath}/student/add">添加学生</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {border-collapse:collapse;}
table,th,td {border: 1px solid black;}
</style>
</head>
<body>
欢迎!
<table>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
<th>地址</th>
</tr>
<c:forEach items="${data}" var="student" >
<tr>
<td>${student.name}</td>
<td>${student.sex}</td>
<td>${student.age}</td>
<td>${student.phone}</td>
<td>${student.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
添加页面!
${id}
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
错误页面!404
</body>
</html>
最后
以上就是老迟到钥匙最近收集整理的关于SpringMVC配置,controller通过注解方式-01的全部内容,更多相关SpringMVC配置内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复