【 Struts2 配置】Struts2基本搭建
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="struts2-hibernate" extends="struts-default" namespace="/"> <action name="student_*" class="k.action.StudentAction" method="{1}"> <result name="list" type="redirectAction">student_list</result> <result name="toList">/WEB-INF/student/list.jsp</result> <result name="toAdd">/WEB-INF/student/toAdd.jsp</result> <result name="toUpdate">/WEB-INF/student/toUpdate.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>k.util.StartSystemListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
StudentAction
package k.action; import com.opensymphony.xwork2.ActionSupport; import k.domain.Student; import k.service.StudentService; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest; import java.util.List; public class StudentAction extends ActionSupport { public String list() { StudentService service = new StudentService(); List list = service.list(); HttpServletRequest request = ServletActionContext.getRequest(); if (list == null || list.size() <= 0) { list.add(new Student("默认", 1)); } request.setAttribute("list", list); return "toList"; } public String toAdd() { return "toAdd"; } public String add() { HttpServletRequest request = ServletActionContext.getRequest(); String name = request.getParameter("name"); String age = request.getParameter("age"); StudentService service = new StudentService(); service.add(new Student(name, Integer.parseInt(age))); return "list"; } public String toUpdate() { HttpServletRequest request = ServletActionContext.getRequest(); String id = request.getParameter("id"); StudentService service = new StudentService(); Student model = service.getModel(id); request.setAttribute("stu", model); return "toUpdate"; } public String update() { HttpServletRequest request = ServletActionContext.getRequest(); String id = request.getParameter("id"); String name = request.getParameter("name"); String age = request.getParameter("age"); Student student = new Student(name, Integer.parseInt(age)); student.setId(Integer.parseInt(id)); StudentService service = new StudentService(); service.update(student); return "list"; } public String delete() { HttpServletRequest request = ServletActionContext.getRequest(); String id = request.getParameter("id"); StudentService service = new StudentService(); Student model = service.getModel(id); service.delete(model); return "list"; } }
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <html> <head> <title>list</title> </head> <body> <h1><a href="${APP_PATH}/student_toAdd.action">student_toAdd</a></h1> <form> <table> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>update</th> <th>delete</th> </tr> </thead> <tbody> <c:forEach items="${list}" var="stu"> <tr> <td>${stu.id}</td> <td>${stu.name}</td> <td>${stu.age}</td> <td><a href="${APP_PATH}/student_toUpdate.action?id=${stu.id}">update</a></td> <td><a href="${APP_PATH}/student_delete.action?id=${stu.id}">delete</a></td> </tr> </c:forEach> </tbody> </table> </form> </body> </html>
toAdd.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>add</title> </head> <body> <form action="${APP_PATH}/student_add.action" method="post"> <h2><input type="text" name="name" value="测试1"></h2> <h2><input type="text" name="age" value="11"></h2> <h2><input type="submit" value="submit"></h2> </form> </body> </html>
toUpdate.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>update</title> </head> <body> <form action="${APP_PATH}/student_update.action" method="post"> <h2><input type="text" name="id" value="${stu.id}"></h2> <h2><input type="text" name="name" value="${stu.name}"></h2> <h2><input type="text" name="age" value="${stu.age}"></h2> <h2><input type="submit" value="submit"></h2> </form> </body> </html>