Rendering: The interface is very simple, no modification is made to the interface
Structure diagram:
jiekou.java
package abc; public interface jiekou { public int cssjs(int a,int b); //Generate random numbers }
jiekoushixian.java
package abc; import java.util.Random; public class jiekoushixian implements jiekou{ @Override public int cssjs(int a, int b) { // TODO Auto-generated method stub Random rand = new Random(); int zhi; // zhi=rand.nextInt(b)+(a+1); zhi = rand.nextInt(b)%(b-a+1) + a; System.out.println("zhi:"+a); /* for(int i=0; i<10; i++) { System.out.println(rand.nextInt(100) + 1); } */ return zhi; } }
quedingAction.java
package action; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import abc.jiekoushixian; import com.opensymphony.xwork2.ActionSupport; public class quedingAction extends ActionSupport implements ServletRequestAware{ HttpServletRequest request; /** * */ private static final long serialVersionUID = 1L; public String registUser() throws Exception{ String a = request.getParameter("one"); //Get the value given by the foreground in the form of a string String b = request.getParameter("two"); // System.out.println("one:"+a+" two:"+b); jiekoushixian jiekoushixian1 = new jiekoushixian (); // Example implementation int a1=Integer.valueOf(a); //Convert the obtained data in string form to int int b1=Integer.valueOf(b); // System.out.println("(int)_one:"+a+" two:"+b); int zhi = jiekoushixian1.cssjs(a1, b1); //Call the method to find random numbers String zhi1 = String.valueOf(zhi); System.out.println("action:"+zhi1); request.setAttribute("kind", zhi1); //Pass the requested random number to the foreground request.setAttribute("one", a); request.setAttribute("two", b); request.setAttribute("frist", "YES"); //The frist variable passed to the foreground is YES, indicating that the index interface is not displayed for the first time return "regist"; } @Override public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; } }
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 ="ch10" namespace="/user" extends="struts-default"> <action name = "regist" class = "action.quedingAction" method = "registUser"> <result name = "regist">/index.jsp</result> </action> </package> </struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="user/regist" method="post"> selection range <!-- <input type = "text" name = "zhi" value = "53"> --> <% int a=1; String frist = (String)request.getAttribute("frist"); if(frist!="YES"){ System.out.println("NO"); %> <input type="text" name="one">~<input type = "text" name = "two"><br> <input type="submit" value="确定" /><br> result: <input type = "text" name = "zhi" > <% } else if(frist.equals("YES")){ String kind = (String)request.getAttribute("kind"); System.out.println("jsp:"+kind); int zhi=Integer.valueOf(kind); String Sone = (String)request.getAttribute("one"); String Stwo = (String)request.getAttribute("two"); int ion = Integer.valueOf(Sone); int itwo=Integer.valueOf(Stwo); %> <input type="text" name="one" value = "<%=ione%>">~<input type = "text" name = "two" value = "<%=itwo%>"><br> <input type="submit" value="确定" /><br> result: <input type = "text" name = "zhi" value = "<%=zhi%>"> <% } %> </form> </body> </html> <!-- <form action="user/regist" method="post"> --> <!-- <input type="submit" value="Submit" /> --> <!-- </form> -->