• JSP之错误信息提示


    MessageResource.properties配置文件:

    RegisterAction注册:

    package com.caiduping.action;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    
    import cn.itcast.web.formbean.UserFormbean;
    
    public class RegisterAction extends Action {
    
    	//actionServlet
    	@Override
    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws Exception {	
    		String param = mapping.getParameter();
    		System.out.println(param);
    		UserFormbean bean = (UserFormbean) request.getSession().getAttribute("UserFormBean");
    		System.out.println(bean.getUsername());
    		System.out.println(bean.getPassword());
    		System.out.println(bean.getEmail());
    		try{
    			System.out.println("向数据注册用户!!");
    			request.setAttribute("message", "注册成功");
    		}catch (Exception e) {
    			request.setAttribute("message", "注册失败");
    		}
    		
    		return mapping.findForward("message");
    	}
    
    	
    	
    }
    

    UserFormbean表格:

    package com.caiduping.formbean;
    
    import java.util.ResourceBundle;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.util.MessageResources;
    
    
    //actionServlet
    public class UserFormbean extends ActionForm {
    
    	private String username;
    	private String password;
    	private String email;
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	//actionServlet
    	@Override
    	public ActionErrors validate(ActionMapping mapping,
    			HttpServletRequest request) {
    		
    		ActionErrors errors = new ActionErrors();
    		if(this.username==null || this.username.trim().equals("")){
    			errors.add("username", new ActionMessage("用户名不能为空",false));
    			errors.add("username", new ActionMessage("errors.username.required"));	
    			errors.add("username", new ActionMessage("errors.required","用户名"));	
    			MessageResources mr = MessageResources.getMessageResources("cn.itcast.resource.MessageResource");
    			errors.add("username", new ActionMessage("errors.required",mr.getMessage("prompt.username")));
    		}
    		return errors;
    	}
    	
    	
    	
    }
    

    页面不存在404.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP '404.jsp' starting page</title>
      </head>
      
      <body>
        对不起,您要访问资源不存在
      </body>
    </html>
    

    index.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'index.jsp' starting page</title>
      </head>
      
      <body>
       	<a href="${pageContext.request.contextPath }/RegisterUI.do">注册</a>
       	
       	<c:url var="register" value="/RegisterUI.do"/>
       	<a href="${register }">注册</a>
       	
       	
       	<% 
       		request.setAttribute("name","中国");
       	%>
       	<html:link action="/RegisterUI" paramId="name" paramName="name">注册</html:link>
       	
       	
       	<% 
       		Map map = new HashMap();
       		map.put("aa","1");
       		map.put("bb","2");
       		map.put("cc","3");
       		
       		request.setAttribute("map",map);
       	%>
       	
       	<html:link action="/RegisterUI" name="map">注册</html:link>
       	
       	<% 
       		request.setAttribute("now",new Date());
       	%>
       	<html:link action="/RegisterUI" paramId="now" paramName="now" paramProperty="time">注册</html:link>
       	
      </body>
    </html>
    

    传递消息message.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'message.jsp' starting page</title>
      </head>
      
      <body>
        ${message }
      </body>
    </html>
    

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
    	xmlns="http://java.sun.com/xml/ns/j2ee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      
      <servlet>
      	<servlet-name>ActionServlet</servlet-name>
      	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      	 <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
      	
      </servlet>
      
      <servlet-mapping>
      	<servlet-name>ActionServlet</servlet-name>
      	<url-pattern>*.do</url-pattern>
      </servlet-mapping>
      
    
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    run:

      

    不努力,还要青春干什么?
  • 相关阅读:
    QEMU编译及使用方法
    C++中的算法
    C++继承
    gcc savetemps选项
    C++ overload、override、overwrite
    拷贝构造函数与拷贝赋值
    C++中的顺序容器
    C++中的虚函数(1)
    C++中lambda的实现(1)
    正确的时间做适合的事
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/5586912.html
Copyright © 2020-2023  润新知