SpringMVC的工作原理与struts2的工作原理差不多,但比struts2更简洁点
1. 导jar包
2. 创建自己起名的spring-servlet.xml文件
命名格式 名称-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- SpringMVC的注解已经注入 具体到扫描包-->
<context:component-scan base-package="controller"/>
<!-- SpringMVC中的controller中return的值传递给本xml文件,通过该下方式,拼接成相应jsp路径
例如,return 个welcome,直接跳转到/WEB-INF/jsp/welcome.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3. web.xml编写配置 (servlet-name中是自己起名的那个名称)
<servlet>
<servlet-name>renjia</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>renjia</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.编写jsp与Controller
index.jsp
<!-- jsp中表单提交传递action值到controller中,去找相应的注解,去调用对应的方法 -->
<form action="haha" method="post">
action(${action})<br>
姓名:<input type="text" name="name"/><br/>
年龄:<input type="text" name="age"/><br/>
参数:<input type="text" name="str1"/><br/>
<input type="submit" value="提交"/>
</form>
welcome.jsp
<body>
<!-- EL表达式接受action传来的参数 -->
welcome!!!${student.name}--->${student.age}---->${str1}
</body>
MyController.class
package controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import bean.Student;
@Controller
public class MyController{
//1将index页表格信息传入welcome页,用Map向页面传参数
@RequestMapping("/haha")
public String hello(Student student,String str1,Map<String,Object> context){
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(str1);
context.put("student",student);
context.put("str1",str1);
return "welcome";
}
//2接受index页面传过来的三个参数 跟index中的name一样
@RequestMapping("/hehe")
public String nihao(HttpServletRequest request,Model model){
System.out.println(request.getParameter("name"));
System.out.println(request.getParameter("age"));
System.out.println(request.getParameter("str1"));
//将student对象和页面传过来的str1放在model里作为一个整体的模型
Student student1 = new Student();
student1.setName(request.getParameter("name"));
student1.setAge(Integer.valueOf(request.getParameter("age")));
model.addAttribute("student",student1);
model.addAttribute("str1", request.getParameter("str1"));
return "welcome";
}
//3用字段的注解的形式创建字段对象,接收index页面传来的字段信息
@RequestMapping("/heihei")
public String hello1(@RequestParam(value="name") String name,
@RequestParam(value="age") int age,
@RequestParam(value="str1") String str1,
Map<String,Object> context){
//想让传递的参数显示在目标的话,传递方式(例Map)需要在传递前就要创建好,否则就会报500空指针
//不能在函数里新创个Map对象来put值再显示在页面
System.out.println(name);
System.out.println(age);
System.out.println(str1);
Student student = new Student();
student.setAge(age);
student.setName(name);
context.put("student", student);
context.put("str1", str1);
return "welcome";
}
//直接用模型注解,将传来的东西给塑造成一个模型给传过来
@RequestMapping("/hengheng")
public String hello2(@ModelAttribute Student student,String str1,
Map<String,Object> context){
System.out.println(student.getAge());
System.out.println(student.getName());
System.out.println(str1);
context.put("student", student);
context.put("str1", str1);
return "welcome";
}
}