struts 韩顺平


基于struts的登录验证:
1.新建web项目;
2.将struts的开发包加入web工程
3.编写login.jsp
4.编写ActionForm(用户表单)Action分控制器;
5.编写struts-config.xml文件,该文件用于配置action actionform的对应关系,和跳转位置,一般放在WEB-INF目录下
6.编写成功失败页面jsp;
7.配置web.xml文件,在该文件中配置struts-config.xml文件
8.测试ok

扩展
1.在登陆成功之后显示用户名;
2.使用filter配合struts解决中文乱码;
   写一个过滤器,并设置request.setCharacterEncoding("utf-8");

  1. public class MyFilter extends HttpServlet implements Filter {
  2. @Override
  3. public void doFilter(ServletRequest request, ServletResponse response,
  4. FilterChain filter) throws IOException, ServletException {
  5. // TODO Auto-generated method stub
  6. request.setCharacterEncoding("utf-8");//设置编码
  7. filter.doFilter(request, response);//必须
  8. }
  9. @Override
  10. public void init(FilterConfig arg0) throws ServletException {
  11. // TODO Auto-generated method stub
  12. }
  13. }
    在web.xml文件中配置过滤器:
  1. <!-- 配置filter -->
  2. <filter>
  3. <filter-name>MyFilter</filter-name>
  4. <filter-class>filter.MyFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>MyFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>
3.struts-config.xml 
4.scope:
5.每个action都是单态的,包括ActionServlet;
   单态:指在程序运行过程中始终只有一个实例对象;
6.struts-config.xml 不一定在WEB-INF目录下,也不一定只有一个只需在web.xml文件配置即可.
  1. <init-param>
  2. <param-name>config</param-name>
  3. <param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-2.xml</param-value>
  4. </init-param>
7.....

基于struts的用户注册验证:
1.导入struts标签库;
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <html:form action="/register.do" method="post" >
  12. 用户:<html:text property="username"></html:text><br/>
  13. 密码:<html:text property="password"></html:text><br/>
  14. 邮箱:<html:text property="email"></html:text><br/>
  15. <html:submit value="注册"></html:submit>
  16. <html:cancel value="取消"></html:cancel>
  17. </html:form>
  18. </body>
  19. </html>
注意:这里的用户、密碼、郵箱必须在<html:form>中并且在form中有相应的字段与之对应.否则会报500错误.因为struts默认认为你的输入框等控件是要用来提交的,所以,必须写在form中且有相应的form类与之对应.而不是在你提交的时候才检测.
2.在web.xml中添加字符编码过滤器Filter,在配置文件中设置字符编码可以使程序更加灵活,也更利于维护;
  1. <filter>
  2. <filter-name>MyFilter</filter-name>
  3. <filter-class>com.zky.struts.filter.MyFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>utf-8</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>MyFilter</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>
  1. public class MyFilter extends HttpServlet implements Filter {
  2. private String encoding;
  3. @Override
  4. public void doFilter(ServletRequest request, ServletResponse response,
  5. FilterChain chain) throws IOException, ServletException {
  6. // TODO Auto-generated method stub
  7. request.setCharacterEncoding(encoding);
  8.        chain.doFilter(request, response);
  9. }
  10. @Override
  11. public void init(FilterConfig config) throws ServletException {
  12. // TODO Auto-generated method stub
  13. encoding = config.getInitParameter("encoding");
  14. }
  15. }
3.在web.xml中添加关键字过滤器SafeFilter,虽然在Action中也可以实现关键字过滤,但是在filter中实现更好;
  1. <filter>
  2. <filter-name>SafeFilter</filter-name>
  3. <filter-class>com.zky.struts.filter.SafeFilter</filter-class>
  4. <init-param>
  5. <param-name>keywords</param-name>
  6. <param-value>拉登;小明;XXX;西吧</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>SafeFilter</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>
java代码:
  1. public class SafeFilter extends HttpServlet implements Filter {
  2. private String[] keywords;
  3. @Override
  4. public void doFilter(ServletRequest request, ServletResponse response,
  5. FilterChain chain) throws IOException, ServletException {
  6. // TODO Auto-generated method stub
  7. String username = (String) request.getAttribute("username");
  8. if (username != null) {
  9. for (String keyword : keywords) {
  10. if (keyword.equals(username)) {
  11.                    request.getRequestDispatcher("/WEN-INF/err.jsp").forward(request, response);
  12. return;
  13. }
  14. }
  15. }
  16. chain.doFilter(request, response);
  17. }
  18. @Override
  19. public void init(FilterConfig config) throws ServletException {
  20. // TODO Auto-generated method stub
  21. keywords = config.getInitParameter("keywords").split(";");
  22. }
  23. }
问题:1.在web.xml文件中编辑filter的配置信息是,不小心删除了ActionServlet的部分配置文件,导致404错误;
    2.在获得用户输入的用户名是应使用request.getParameter("username");而不是request.getAttribute("username");
导致过滤器无法使用.


案例:文件的上传下载
* 文件上传需要解决的几个问题:
    1.文件名重复时,后上传的文件会覆盖先上传文件的问题;
    2.中文文件名显示乱码问题;







 




posted @ 2017-03-25 21:25  Mr.xiaobai丶  阅读(684)  评论(0编辑  收藏  举报