个人笔记:
点击链接,跳转到对应的界面时,有两种情况
1、利用InternalResourceViewResolver来实现,跳转到对应名字上的jsp
步骤:
1)配置springmvc的配置文件:
<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
2)点击链接,跳转到handler中,其中handler中的返回值必须在/WEB-INF/view/下,并且如果返回值是success,就跳转到success.jsp中
2、利用BeanNameViewResolver来跳转自定义视图中
步骤:
1)首先自己定义class类,并且必须要有@Component注解,实现view中的getContentType和render,其中getContentType的返回值应该是默认的"text/html"格式的网页,在render中进行绘制视图。
2)配置springmvc的配置文件
<!-- 配置视图 BeanNameViewResolver 解析器: 使用视图的名字来解析视图 -->
<!-- 通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
3)点击链接,跳转到handler,返回值为1)步骤中写的class,首字母小写即可。
详细讲解
1、首先必须要有上一篇我提到的配置文件
2、jsp
1)第一种就是通过form表单进行提交
<form:form action="${pageContext.request.contextPath }/employee/save"
modelAttribute="employee" method="post">
<c:if test="${employee.id==null }">
用户名:<form:input path="lastName" />
<br>
</c:if>
<c:if test="${employee.id!=null }">
<form:hidden path="id"/>
<input type="hidden" value="PUT" name="_method" />
</c:if>
邮箱:<form:input path="email" />
<br>
<%
Map<String, String> map = new HashMap();
map.put("1", "Male");
map.put("0", "Female");
request.setAttribute("genders", map);
%>
性别:<form:radiobuttons items="${genders}" path="gender" />
<br>
部门:<form:select path="department.id" items="${departs }"
itemValue="id" itemLabel="departmentName"></form:select>
<br>
生日:<form:input path="birth"/>
<br>
工资:<form:input path="salary"/>
<br>
<input type="submit" value="提交" />
</form:form>
注意:action当中最好利用绝对路径,否则再后来界面跳转会有问题,并且大部分项目中,多是利用绝对路径
2)通过链接即a标签进行
<a href="employee/emps">员工列表</a>
3、handler
@RequestMapping("/employee")
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
@RequestMapping("/testi18n")
public String testi18n(){
return "i18n";
}
/**
* 返回json对象
* @return
*/
@ResponseBody
@RequestMapping("/testJson")
public Collection<Employee> json(){
return employeeDao.getAll();
}
/*sprinmvc.xml
1、配置视图
2、配置静态资源访问时的路径 default
3、自动扫描包*/
/**
* 从数据库中获取数据
* @param id
* @param map
*/
@ModelAttribute
public void getValue(@RequestParam(value="id",required=false) Integer id,Map<String, Object> map){
if(id!=null){
map.put("departs", departmentDao.getDepartments());
map.put("employee", employeeDao.get(id));
}
}
/**
* 修改操作
* @param id
* @param map
* @return
*/
@RequestMapping(value="/save",method=RequestMethod.PUT)
public String updateAndSave(Employee employee){
employeeDao.save(employee);
return "redirect:/employee/emps";
}
/**
* 进入修改页面,回显数据
* @param id
* @param map
* @return
*/
@RequestMapping(value="/update/{id}")
public String update(@PathVariable(value="id") Integer id,Map<String, Object> map){
map.put("departs", departmentDao.getDepartments());
map.put("employee", employeeDao.get(id));
return "input";
}
/**
* 删除用户信息
* @param id
* @return
*/
@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable(value="id") Integer id){
employeeDao.delete(id);
return "redirect:/employee/emps";
}
/**
* 进行保存员工信息
* @return
*/
@RequestMapping("/save")
public String save(@Valid Employee employee){
System.out.println("save"+employee);
employeeDao.save(employee);
return "redirect:/employee/emps";
}
/**
* 展示所有部门信息
* @return
*/
@RequestMapping("/input")
public String departs(Map<String, Object> map){
map.put("departs", departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
/**
* 显示所有员工的信息
* @return
*/
@RequestMapping("/emps")
public String emps(Map<String, Object> map){
map.put("employee", employeeDao.getAll());
return "list";
}
}
其中类中必须要用@controller进行注解,表示该类作为handler,@requestMapping可以进行标识类和方法,在2中无论是action和a中的链接,第一个employee就是利用类中的requestMapping中的value值,第二个就是方法中的requestMapping中的value值。