Struts2_实战演练(上)
三个准备:
1.导入Struts2库(jar包)
2.添加核心配置文件struts.xml
3.配置properties文件
准备二,配置struts2核心filter
|
Web.xml |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<filter> <filter-name>filterDispatcher</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter>
<filter-mapping> <filter-name>filterDispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
</web-app>
|
准备三:配置properties文件
|
struts.properties |
|
struts.action.extension=do |
第一个sturts框架
登录框:
|
Index.jsp |
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">
<title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head>
<body> 用户登录 <form action="login.do" name="userform" id="userform"> 用户姓名:<input type="text" name="userName" id="uname"/> <br/> 用户密码:<input type="password" name="userPwd" id = "pwd"/> <br/> <input type="submit" value="用户登录"/> </form> </body> </html>
|
登录跳转需经过struts配置文件
|
Struts.xml |
||
|
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="loginpackage" extends="struts-default"> <action name="login" class="com.softeee.action.LoginAction"> <result name="success">success.jsp</result> </action> </package>
<!-- 在主配置文件中可以包含分支文件 --> <include file="book.xml"></include>
<include file="auth.xml"></include>
<include file="commoper.xml"></include> </struts>
|
通过配置文件跳转到LoinAction.java中并找到默认的execute方法
|
LoginAction.java |
|
package com.softeee.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware{
Map session;
//用户姓名 private String userName; //用户密码 private String userPwd;
public LoginAction() { System.out.println("实例化了"); }
public String execute() throws Exception{ System.out.println("登录的用户名是:"+userName+" 密码:"+userPwd); //session中存入用户信息 session.put("uname",userName); return SUCCESS; }
public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; }
public void setSession(Map<String, Object> arg0) { this.session=arg0; }
}
|
搜索书
|
Search.jsp |
|||
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">
<title>My JSP ‘searchbook.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body> 查询书本信息 <form action="search.do" name="searchform" id="searchform"> 书名:<input type="text" name="book.bookName" id="bookName"> <input type="submit" value="查询"> </form> </body> </html>
|
|||
|
BookOperAction.Java |
|||
package com.softeee.action;
import java.util.ArrayList; import java.util.List;
import com.opensymphony.xwork2.ActionSupport; import com.ibm.dto.Book; import com.ibm.factory.HibernateSessionFactory;
public class BookOperAction extends ActionSupport {
private Book book;
private List<Book> bookList;
public List<Book> getBookList() { return bookList; }
public void setBookList(List<Book> bookList) { this.bookList = bookList; }
public Book getBook() { return book; }
public void setBook(Book book) { this.book = book; }
public String searchBook(){ System.out.println("查询的书名是:"+book.getBookName());
HibernateSessionFactory.getSession();
//以下部分模拟从数据库中查询书本并存放到list对象中进行传递 bookList = new ArrayList<Book>(); for (int i = 0; i < 5; i++) { Book book = new Book(); book.setBookId(i); book.setBookName("孙子兵法"+i); bookList.add(book); }
return "success"; }
public String delBook(){ System.out.println("删除的书本的编号是:"+book.getBookId()); //补全删除部分的代码 return "success"; }
public String updateBook(){ System.out.println("修改的书本的编号是:"+book.getBookId()); //伪代码部分 真实应用中从数据库中查询与之对应的结果 book=new Book(); book.setBookId(1); book.setBookName("货币战争"); return "success"; }
public String upBook(){ //调用hibernate的dao完善此部分 System.out.println("修改部分"); return "success"; }
}
|
配置通配符
1.当struts2解析到adduser方法时,就会自动去找user的add方法
2.直接转向到adduser.jsp
|
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="useroperpackage" extends="struts-default"> <action name="*user" class="com.softeee.action.UserOperAction" method="{1}user">(“1”代表第一个“*”) <result name="success">{1}user.jsp</result> </action> </package>
</struts> |
java 从零开始,学习笔记之基础入门<Struts2_实战演练(上)>(三十八)
原文:http://blog.csdn.net/rulon147/article/details/19190165