導航:首頁 > 編程語言 > javarender

javarender

發布時間:2022-09-05 04:00:18

A. 求java畫圖版,能畫正多邊形的代碼!

public class MiniDraw implements ActionListener, MouseListener{ // Fields private JFrame frame = new JFrame("MiniDraw");
private DrawingCanvas canvas = new DrawingCanvas(); private JButton colorButton; // the button for colour, so we can change its background to the current color.
private JTextField textField; // the field for entering text to put on the canvas // fields for recording the information needed to perform the next action
private enum Action {Line,Rect, Oval, Triangle, Text, Dot, Move, Delete, Push, Pull}; private int pressedX; //where the mouse was pressed
private int pressedY; //where the mouse was pressed
private Action currentAction = Action.Line;
private Color currentColor = Color.black; private List<Shape> shapes = new ArrayList <Shape>();

/** Constructor sets up the GUI. */
public MiniDraw(){
frame.setSize(800,600); //The graphics area
canvas.addMouseListener(this);
frame.getContentPane().add(canvas, BorderLayout.CENTER); //The buttons
JPanel topPanel = new JPanel( );
frame.getContentPane().add(topPanel, BorderLayout.NORTH);
JPanel botPanel = new JPanel( );
frame.getContentPane().add(botPanel, BorderLayout.SOUTH);
addButton(topPanel, "New");
addButton(topPanel, "Open");
addButton(topPanel, "Save");
addButton(topPanel, "Delete");
addButton(topPanel, "Move");
colorButton = addButton(topPanel, "Color"); // remember the button so we can change its colour
addButton(topPanel, "Push");
addButton(topPanel, "Pull");
addButton(topPanel, "Reverse");
addButton(topPanel, "Quit"); botPanel.add(new JLabel("Shapes: "));
addButton(botPanel, "Line");
addButton(botPanel, "Rect");
addButton(botPanel, "Oval");
addButton(botPanel, "Triangle");
addButton(botPanel, "Dot");
addButton(botPanel, "Text");
textField = new JTextField("Enter text", 12);
botPanel.add(new JLabel(" : "));
botPanel.add(textField); frame.setVisible(true);
} /** Utility method to make new button and add it to the panel
Returns the button, in case we need it. */
private JButton addButton(JPanel panel, String name){
JButton button = new JButton(name);
button.addActionListener(this);
panel.add(button);
return button;
} /** Respond to button presses */ public void actionPerformed(ActionEvent e){
String button = e.getActionCommand();
//System.out.printf("Button: %s\n", button); //for debugging
if (button.equals("New") )
newDrawing();
else if (button.equals("Open") )
openDrawing();
else if (button.equals("Save") )
saveDrawing();
else if (button.equals("Color") )
selectColor();
else if (button.equals("Reverse") )
reverseDrawing();
else if (button.equals("Quit") )
frame.dispose();
else{ //
currentAction = Action.valueOf(button); // converts the String to an Action.
}
} /** Sets the current color.
* Also changes the color of the Color button and sets the background color of the canvas */
private void selectColor(){
Color newColor = JColorChooser.showDialog(frame,"Choose Color for new shapes", currentColor);
if (newColor!=null){
currentColor=newColor;
colorButton.setBackground(currentColor);
}
} // Respond to mouse events
/** When mouse is pressed, remember the position in order to construct the Shape when
* the mouse is released. */
public void mousePressed(MouseEvent e) {
int x = e.getX(); int y = e.getY();
//System.out.printf("Pressed at (%d, %d)\n", x, y); //for debugging
pressedX = x;
pressedY = y;
} /** When the Mouse is released, depending on the currentAction,
* either construct the shape that was being drawn, or perform the
* action (delete or move) on the shape under the point where the mouse was pressed.*/

public void mouseReleased(MouseEvent e) {
int x = e.getX(); int y = e.getY();
//System.out.printf("Released at (%d, %d)\n", x, y); //for debugging
if (currentAction==Action.Move)
moveShape(pressedX, pressedY, x, y);
else if (currentAction==Action.Delete)
deleteShape(x, y);
else if (currentAction==Action.Push)
pushShapeBackward(x, y);
else if (currentAction==Action.Pull)
pullShapeForward(x, y);
else
addShape(pressedX, pressedY, x, y);
} public void mouseClicked(MouseEvent e) {} //needed to satisfy interface
public void mouseEntered(MouseEvent e) {} //needed to satisfy interface
public void mouseExited(MouseEvent e) {} //needed to satisfy interface
// Helper methods for implementing the button and mouse actions

/** Start a new drawing. */
public void newDrawing(){
shapes = new ArrayList<Shape>();
canvas.clear();
}
/** Open a file, and read all the shape descriptions into the current drawing. */
public void openDrawing(){
String fname = FileChooser.open();
if (fname==null) return;
try {
Scanner file = new Scanner(new File(fname));
//System.out.printf("Opening file %s\n", fname);
shapes = new ArrayList<Shape>();
while (file.hasNext()){
String shapetype = file.next().toLowerCase();
if (shapetype.equals("oval"))
shapes.add(new Oval(file));
else if (shapetype.equals("rectangle"))
shapes.add(new Rectangle(file));
if (shapetype.equals("line"))
shapes.add(new Line(file));
if (shapetype.equals("textshape"))
shapes.add(new TextShape(file));
else if (shapetype.equals("triangle"))
shapes.add(new Triangle(file));
else if (shapetype.equals("dot"))
shapes.add(new Dot(file));
}
render();
}
catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
} /** Save the current drawing to a file. */
public void saveDrawing(){
String fname =FileChooser.save();
if ( fname == null )
return;
try{
PrintStream f = new PrintStream( new File( fname ));
for ( Shape shape : shapes )
f.println( shape.toString());
f.close ();
}
catch(IOException ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
/** Returns the shape under the position (x y), or null if no such shape */
public Shape findShape(int x, int y){
for (int i = shapes.size()-1; i>=0; i--){ // must go backwards, so can't use foreach
Shape shape = shapes.get(i);
if (shape.pointOnShape(x, y))
return shape;
}
return null; // no shape found at position
}
/** Moves the shape that was under the mousePressed position (pressedX, pressedY)
to where the mouse was released.
Ie, move it by (newX-pressedX) and (newY-pressedY)
*/
public void moveShape(int fromX, int fromY, int toX, int toY){
//System.out.printf("Moving shape under (%d, %d) to (%d, %d)\n", pressedX, pressedY, newX, newY); //for debugging
Shape shape = findShape(fromX, fromY);
if (shape!= null)
shape.moveBy((toX-fromX), (toY-fromY));
render();
} /** Deletes the shape that was under the mouseReleased position (x, y)
*/
public void deleteShape(int x, int y){ for ( Shape shape : shapes) {
if ( shape.pointOnShape(x, y ))
shapes.remove ( shape );
}
render();
}
/** Pull the shape under the mouse one position closer to the "front"
of the drawing */
public void pullShapeForward(int x, int y){
// System.out.printf("Pulling shape under (%d, %d) forward\n", x, y); //for debugging
// YOUR CODE HERE
Shape shape = findShape( x, y );
if (shape!=null ) {
int index = shapes.indexOf(shape);
if ( index< shapes.size()-1){
shapes.remove(index);
shapes.add( index+1,shape);
}
render();

}
}
/** Push the shape under the mouse one position further from the "front"
of the drawing */
public void pushShapeBackward(int x, int y){
//System.out.printf("Pushing shape under (%d, %d) backward\n", x, y); //for debugging
// YOUR CODE HERE
Shape shape = findShape(x, y );
if ( shape!=null ){
int index = shapes.indexOf(shape);
if( index > 0 )
shapes.remove( index);
shapes.add( index-1, shape);
}
render();

}

/** Reverse the order of the shapes in the drawing */
public void reverseDrawing(){ List<Shape>temp = new ArrayList<Shape>();
while ( shapes.size() > 0 ) {
temp.add ( shapes.remove(shapes.size()-1));
}
shapes= temp;
render();

}
/** Construct a new Shape object of the appropriate kind (depending on currentAction)
Uses the appropriate constructor of the Line, Rectangle, Oval, TextShape, or Triangle classes.
adds the shape it to the collection of shapes in the drawing, and
renders the shape on the canvas */
public void addShape(int x1, int y1, int x2, int y2){
//System.out.printf("Drawing shape %s, at (%d, %d)-(%d, %d)\n", currentAction, pressedX, pressedY, x, y); //for debugging
Shape shape = null;
if (currentAction==Action.Line)
shapes.add(new Line(x1, y1, x2, y2, currentColor));
if (currentAction==Action.Triangle)
shapes.add(new Triangle(x1, y1, x2, y2, currentColor));
if (currentAction==Action.Dot)
shapes.add(new Dot(x2, y2, currentColor));
else if (currentAction==Action.Text)
shapes.add(new TextShape(x2, y2, textField.getText(), currentColor));
else{
int left= Math.min(x1, x2);
int top= Math.min(y1, y2);
int width= Math.abs(x1 - x2);
int height= Math.abs(y1 - y2);
if (currentAction==Action.Rect)
shapes.add(new Rectangle(left, top, width, height, currentColor));
else if (currentAction==Action.Oval)
shapes.add(new Oval(left, top, width, height, currentColor));
}
render();
} public void render(){
canvas.clear(false);
for (Shape shape : shapes){
shape.render(canvas);
}
canvas.display();
} public static void main(String args[]){
new MiniDraw();
}}

B. 求後台運行java方法向前台推數據例子

play框架:

/**後台**/
Map<String,Object> queryStudent = Stock.queryStudent();//返回一個queryStudent的map
render(queryStudent );//render這個queryStudent 。

<!-- 頁面 -->
學生一共有${queryStudent ?.sumId}人<!-- 頁面上引用就好了 -->

C. 什麼是java過濾器! 它的功能和作用是什麼啊

Filter 技術是servlet 2.3 新增加的功能.servlet2.3是sun公司與2000年10月發布的,它的開發者包括許多個人和公司團體,充分體現了sun公司所倡導的代碼開放性原則.由於眾多的參與者的共同努力,servlet2.3比以往功能都強大了許多,而且性能也有了大幅提高.
它新增加的功能包括:
1. 應用程序生命周期事件控制;
2. 新的國際化;
3. 澄清了類的裝載規則;
4. 新的錯誤及安全屬性;
5. 不贊成使用HttpUtils 類;
6. 各種有用的方法;
7. 闡明並擴展了幾個servlet DTD;
8. filter功能.
其中最重要的就是filter功能.它使用戶可以改變一個request和修改一個 response. Filter 不是一個servlet,它不能產生一個response,它能夠在一個request到達servlet之前預處理request,也可以在離開 servlet時處理response.換種說法,filter其實是一個」servlet chaining」(servlet 鏈).一個filter 包括:
1. 在servlet被調用之前截獲;
2. 在servlet被調用之前檢查servlet request;
3. 根據需要修改request頭和request數據;
4. 根據需要修改response頭和response數據;
5. 在servlet被調用之後截獲.
你能夠配置一個filter 到一個或多個servlet;單個servlet或servlet組能夠被多個filter 使用.幾個實用的filter 包括:用戶辨認filter,日誌filter,審核filter,加密filter,符號filter,能改變xml內容的XSLT filter等.
一個filter必須實現javax.servlet.Filter介面並定義三個方法:
1.void setFilterConfig(FilterConfig config) //設置filter 的配置對象;
2. FilterConfig getFilterConfig() //返回filter的配置對象;
3. void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) //執行filter 的工作.
伺服器每次只調用setFilterConfig方法一次准備filter 的處理;調用doFilter方法多次以處理不同的請求.FilterConfig介面有方法可以找到filter名字及初始化參數信息.伺服器可以設置 FilterConfig為空來指明filter已經終結.
每一個filter從doFilter()方法中得到當前的request及 response.在這個方法里,可以進行任何的針對request及response的操作.(包括收集數據,包裝數據等).filter調用 chain.doFilter()方法把控制權交給下一個filter.一個filter在doFilter()方法中結束.如果一個filter想停止 request處理而獲得對response的完全的控制,那它可以不調用下一個filter.
一個filter可以包裝request 或response以改變幾個方法和提供用戶定製的屬性.Api2.3提供了HttpServletRequestWrapper 和HttpServletResponseWrapper來實現.它們能分派最初的request和response.如果要改變一個方法的特性,必須繼承wapper和重寫方法.下面是一段簡單的日誌filter用來記錄所有request的持續時間.
public class LogFilter implements Filter {
FilterConfig config;
public void setFilterConfig(FilterConfig config) {
this.config = config;
}
public FilterConfig getFilterConfig() {
return config;
}
public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain) {
ServletContext context = getFilterConfig().getServletContext();
long bef = System.currentTimeMillis();
chain.doFilter(req, res); // no chain parameter needed here
long aft = System.currentTimeMillis();
context.log("Request to " + req.getRequestURI()
+ ": " + (aft-bef));
}
}
當server調用setFilterConfig(),filter保存config信息. 在doFilter()方法中通過config信息得到servletContext.如果要運行這個filter,必須去配置到web.xml中.以 tomcat4.01為例:
<filter>
<filter-name>
log //filter 名字
</filter-name>
<filter-class>
LogFilter //filter class(上例的servlet)
</filter-class>
</filter>
<filter-mapping>
<filter-name>log</filter-name>
<servletname>servletname</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>servletname</servletname>
<servletclass>servletclass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletname</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
把這個web.xml放到web-inf中(詳請參考tomcat幫助文檔).
當每次請求一個request時(如index.jsp),先到LogFilter中去並調用doFilter()方法,然後才到各自的servlet中去.如果是一個簡單的servlet(只是一個頁面,無任何輸出語句),那麼可能的輸出是:
Request to /index.jsp: 10
Filter是一個COM組件,由一個或多個Pin組成。Pin也是一個COM組件。 Filter文件的擴展名為.ax,但也可以是.dll。Filter根據其包含Input pin或Output pin的情況(或在Filter Graph的位置),大致可分為三類:Source Filter(僅有Output pin)、Transform Filter(同時具有Input pin和Output pin)和Renderer Filter(僅有Input pin)。
一般情況下,創建Filter使用一個普通的Win32 DLL項目。而且,一般Filter項目不使用MFC。這時,應用程序通過CoCreateInstance函數Filter實例;Filter與應用程序在二進制級別的協作。另外一種方法,也可以在MFC的應用程序項目中創建Filter。這種情況下,Filter不需注冊為COM組件,Filter與應用程序之間的協作是源代碼級別的;創建Filter實例,不再使用CoCreateInstance函數,而是直接new出一個Filter對象,如下:
m_pFilterObject = new CFilterClass();
// make the initial refcount 1 to match COM creation
m_pFilterObject ->AddRef();
因為Filter的基類實現了對象的引用計數,所以即使在第二種情況下,對創建後的Filter對象的操作也完全可以遵循COM標准。
Filter是一個獨立功能模塊,最好不要將Filter依賴於其他第三方的DLL。因為 Filter具有COM的位置透明性特點,Filter文件可以放在硬碟的任何位置,只要位置移動後重新注冊。但此時,如果Filter依賴其他DLL,則Filter對該DLL的定位就會出現問題。
Filter不能脫離Filter Graph單獨使用。所以,如果你想繞過Filter Graph直接使用Filter實現的模塊功能,請將你的Filter移植成DMO(DirectX Media Object)。對於DirectShow應用程序開發者來說,還有一點,請不要忘記使用OleInitialize進行初始化。
2. Filter的注冊
Filter是COM組件,所以在使用前一定要注冊。Filter的注冊程序為 regsvr32.exe。如果帶上命令行參數/u,表示注銷;如果帶上是/s,表示不彈出任何注冊/注銷成功與否的提示對話框。如果你想在Build Filter項目的時候進行自動注冊,請在VC的Project settings的Custom Build頁如下設置:
Description: Register filter
Commands: regsvr32 /s /c $(TargetPath)
echo regsvr32 exe.time > $(TargetDir)\$(TargetName).trg
Outputs: $(TargetDir)\$(TargetName).trg
Filter的注冊信息包括兩部分:基本的COM信息和Filter信息。注冊信息都存放在注冊表中。前者的位置為:HKEY_CLASSES_ROOT\CLSID\Filter Clsid\,後者的位置為:HKEY_CLASSES_ROOT\CLSID\Category\Instance\ Filter Clsid\。COM信息標示了Filter是一個標準的可以通過CoCreateInstance函數創建的COM組件,Filter信息標示了我們通過Graphedit看到的描述這個Filter的信息。如果你不想讓Graphedit看到(或者讓Filter枚舉器找到)你寫的Filter,你完全可以不注冊Filter信息。而且不用擔心,你這么做也完全不會影響Filter的功能。
屏蔽注冊Filter信息的方法也很簡單。因為CBaseFilter實現了IAMovieSetup介面的兩個函數:Register和Unregister。我們只需重載這兩個函數,直接return S_OK就行了。
Filter的Merit值。這個值是微軟的「智能連接」函數使用的。在Graphedit中,當我們加入一個Source Filter後,在它的pin上執行「Render」,會自動連上一些Filter。Merit的值參考如下:
MERIT_PREFERRED = 0x800000,
MERIT_NORMAL = 0x600000,
MERIT_UNLIKELY = 0x400000,
MERIT_DO_NOT_USE = 0x200000,
MERIT_SW_COMPRESSOR = 0x100000,
MERIT_HW_COMPRESSOR = 0x100050
Merit值只有大於MERIT_DO_NOT_USE的時候才有可能被「智能連接」使用;Merit的值越大,這個Filter的機會就越大。
3. Filter之間Pin的連接過程
Filter只有加入到Filter Graph中並且和其它Filter連接成完整的鏈路後,才會發揮作用。Filter之間的連接(也就是Pin之間的連接),實際上是連接雙方的一個 Media type的協商過程。連接的方向總是從Output pin指向Input pin。連接的大致過程為:如果調用連接函數時已經指定了完整的Media type,則用這個Media type進行連接,成功與否都結束連接過程;如果沒有指定或不完全指定了Media type,則進入下面的枚舉過程。枚舉欲連接的Input pin上所有的Media type,逐一用這些Media type與Output pin進行連接(如果連接函數提供了不完全Media type,則要先將每個枚舉出來的Media type與它進行匹配檢查),如果Output pin也接受這種Media type,則Pin之間的連接宣告成功;如果所有Input pin上枚舉的Media type,Output pin都不支持,則枚舉Output pin上的所有Media type,並逐一用這些Media type與Input pin進行連接。如果Input pin接受其中的一種Media type,則Pin之間的連接到此也宣告成功;如果Output pin上的所有Media type,Input pin都不支持,則這兩個Pin之間的連接過程宣告失敗。
每個Pin都可以實現GetMediaType函數來提供該Pin上支持的所有 Preferred Media type(但一般只在Output pin上實現,Input pin主要實現CheckMediaType看是否支持當前提供的Media type就行了)。連接過程中,Pin上枚舉得到的所有Media type就是這里提供的。
在CBasePin類中有一個protected的成員變數 m_bTryMyTypesFirst,默認值為false。在我們定製Filter的Output pin中改變這個變數的值為true,可以定製我們自己的連接過程(先枚舉Output pin上的Media type)。
當Pin之間的連接成功後,各自的pin上都會調用CompleteConnect函數。我們可以在這里取得一些連接上的Media type的信息,以及進行一些計算等。在Output pin的CompleteConnect實現中,還有一個重要的任務,就是協商Filter Graph運行起來後Sample傳輸使用的內存配置情況。這同樣是一個交互過程:首先要詢問一下Input pin上的配置要求,如果Input pin提供內存管理器(Allocator),則優先使用Input pin上的內存管理器;否則,使用Output pin自己生成的內存管理器。我們一般都要實現DecideBufferSize來決定存放Sample的內存大小。注意:這個過程協商完成之後,實際的內存並沒有分配,而要等到Output pin上的Active函數調用。
4. Filter Media type概述
Media type一般可以有兩種表示:AM_MEDIA_TYPE和CMediaType。前者是一個Struct,後者是從這個Struct繼承過來的類。
每個Media type有三部分組成:Major type、Subtype和Format type。這三個部分都使用GUID來唯一標示。Major type主要定性描述一種Media type,比如指定這是一個Video,或Audio或Stream等;Subtype進一步細化Media type,如果Video的話可以進一步指定是UYVY或YUY2或RGB24或RGB32等;Format type用一個Struct更進一步細化Media type。
如果Media type的三個部分都是指定了某個具體的GUID值,則稱這個Media type是完全指定的;如果Media type的三個部分中有任何一個值是GUID_NULL,則稱這個Media type 是不完全指定的。GUID_NULL具有通配符的作用。
常用的Major type:
MEDIATYPE_Video;
MEDIATYPE_Audio;
MEDIATYPE_AnalogVideo; // Analog capture
MEDIATYPE_AnalogAudio;
MEDIATYPE_Text;
MEDIATYPE_Midi;
MEDIATYPE_Stream;
MEDIATYPE_Interleaved; // DV camcorder
MEDIATYPE_MPEG1SystemStream;
MEDIATYPE_MPEG2_PACK;
MEDIATYPE_MPEG2_PES;
MEDIATYPE_DVD_ENCRYPTED_PACK;
MEDIATYPE_DVD_NAVIGATION;
常用的Subtype:
MEDIASUBTYPE_YUY2;
MEDIASUBTYPE_YVYU;
MEDIASUBTYPE_YUYV;
MEDIASUBTYPE_UYVY;
MEDIASUBTYPE_YVU9;
MEDIASUBTYPE_Y411;
MEDIASUBTYPE_RGB4;
MEDIASUBTYPE_RGB8;
MEDIASUBTYPE_RGB565;
MEDIASUBTYPE_RGB555;
MEDIASUBTYPE_RGB24;
MEDIASUBTYPE_RGB32;
MEDIASUBTYPE_ARGB32; // Contains alpha value
MEDIASUBTYPE_Overlay;
MEDIASUBTYPE_MPEG1Packet;
MEDIASUBTYPE_MPEG1Payload; // Video payload
MEDIASUBTYPE_MPEG1AudioPayload; // Audio payload
MEDIASUBTYPE_MPEG1System; // A/V payload
MEDIASUBTYPE_MPEG1VideoCD;
MEDIASUBTYPE_MPEG1Video;
MEDIASUBTYPE_MPEG1Audio;
MEDIASUBTYPE_Avi;
MEDIASUBTYPE_Asf;
MEDIASUBTYPE_QTMovie;
MEDIASUBTYPE_PCM;
MEDIASUBTYPE_WAVE;
MEDIASUBTYPE_dvsd; // DV
MEDIASUBTYPE_dvhd;
MEDIASUBTYPE_dvsl;
MEDIASUBTYPE_MPEG2_VIDEO;
MEDIASUBTYPE_MPEG2_PROGRAM;
MEDIASUBTYPE_MPEG2_TRANSPORT;
MEDIASUBTYPE_MPEG2_AUDIO;
MEDIASUBTYPE_DOLBY_AC3;
MEDIASUBTYPE_DVD_SUBPICTURE;
MEDIASUBTYPE_DVD_LPCM_AUDIO;
MEDIASUBTYPE_DVD_NAVIGATION_PCI;
MEDIASUBTYPE_DVD_NAVIGATION_DSI;
MEDIASUBTYPE_DVD_NAVIGATION_PROVIDER;
常用的Format type:
FORMAT_None
FORMAT_DvInfo DVINFO
FORMAT_MPEGVideo MPEG1VIDEOINFO
FORMAT_MPEG2Video MPEG2VIDEOINFO
FORMAT_VideoInfo VIDEOINFOHEADER
FORMAT_VideoInfo2 VIDEOINFOHEADER2
FORMAT_WaveFormatEx WAVEFORMATEX
5. Filter之間的數據傳送
Filter之間的數據是通過Sample來傳送的。Sample是一個COM組件,擁有自己的一段數據緩沖。Sample由Allocator統一管理。如下圖所示:
Filter之間數據傳送的方式有兩種:Push模式和Pull模式。

D. java jtable表頭名相同時,cellrender渲染器無法設置表頭名相同的列

樓主,具體的render是如何寫的。。。。。。

你上面是getColumnName那有三個列名相同喔,這樣應該可以的了

table.getColumnModel().getColumn(0).setCellRenderer(render);

E. 求一段JAVA代碼要求如下 獲取輸入的欄位和資料庫中的比較如果沒有就把該欄位存入資料庫 資料庫 STUDENT

這個是重復的驗證,你可以在jsp頁面通過Ajax,將輸入的值傳到後台Action,在Action里把該值放入查詢語句到資料庫進行查詢,進行重復判斷。

js代碼如下:
var f = document.forms[0];
$("#role_name").blur(function(){
if(null == f.role_name.value){
return ;
}
$.ajax({
type: "POST",
url: "RoleInfo.do",
data: "method=validateRolename&id=${af.map.id}&role_name=" + f.role_name.value,
dataType: "json",
error: function(request, settings) {},
success: function(oper) {
if(oper.result){
$("#role_name").val("");
alert("系統已存在此角色");
f.role_name.focus();
}else {
return;
}
}
});
Action代碼:
public ActionForward validateRolename(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

DynaBean dynaBean = (DynaBean) form;
StringBuffer oper = new StringBuffer("{result:");
String role_name = (String) dynaBean.get("role_name");
//String id = (String) dynaBean.get("id");

if (!StringUtils.isBlank(role_name)) {
RoleInfo ri = new RoleInfo();
ri.setRole_name(role_name);
//if(null != id){
ri.getMap().put("id_not_in",(String) dynaBean.get("id"));
//}
ri = super.getFacade().getRoleInfoService().getRoleInfo(ri);
if (null == ri) {
oper.append(false);
} else {
oper.append(true);
}
} else {
oper.append("error");
}

oper.append("}");
super.render(response, oper.toString(), "text/x-json;charset=UTF-8");
return null;

}

F. Java中ModelAndView是做什麼的

ModelAndView以org.springframework.ui.ModelMap的形式來保持模型數據,通過構造方法傳入的或者通過實例方法添加的模型數據都將添加到這個ModelMap中。至於ModelMap中保持的模型數據將會在視圖渲染階段,由具體的View實現類來獲取並使用。

我們需要為添加到ModelAndView的一組或者多組模型數據提供相應的鍵(Key),以便具體的View實現類可以根據這些鍵獲取具體的模型數據,然後公開給視圖模板。通常,模型中的數據對應的鍵需要與視圖模板中的標志符相對應,如圖所示:

基於JSP/JSTL模板的視圖實現,通常是將模型數據通過HttpServletRequest的屬性(Attribute)的形式公開給具體的模板。而像基於Velocity之類的通用模板引擎的視圖實現,則會將ModelAndView中的模型數據復制到它們自己的數據獲取上下文中,比如Velocity的Context。

但不管什麼視圖類型,對應的視圖模板都將可以通過添加到ModelAndView的模型數據的鍵來獲取模型數據,並合並到最終的視圖輸出結果中。

拓展資料:

ModelAndView 的構造方法有7個。但是它們都是相通的。這里使用無參構造函數來舉例說明如何構造ModelAndView 實例。

ModelAndView 類別就如其名稱所示,是代表了MVC Web程序中Model與View的對象,不過它只是方便您一次返回這兩個對象的holder,Model與View兩者仍是分離的概念。

最簡單的ModelAndView是持有View的名稱返回,之後View名稱被view resolver,也就是實作org.springframework.web.servlet.View介面的實例解析,例如 InternalResourceView或JstlView等等。

ModelAndView(String viewName) 如果您要返回Model對象,則可以使用Map來收集這些Model對象,然後設定給ModelAndView,使用下面這個版本的 ModelAndView: ModelAndView(String viewName, Map model) Map對象中設定好key與value值。

之後可以在視圖中取出,如果您只是要返回一個Model對象,則可以使用下面這個 ModelAndView版本: ModelAndView(String viewName, String modelName, Object modelObject) 藉由modelName,您可以在視圖中取出Model並顯示。

web.servlet.View的實例,View介面如下: public interface View { public void render(Map model, HttpServletResquest resquest, HttpServletResponse response) throws ServletException, IOException; }

View的實作之前用過 org.springframework.web.servlet.view.InternalResourceView,另外也還有JstlView、 TilesView、VelocityView等等的實作,分別進行不同的表現展處理 。

ModelAndView() 這個構造方法構造出來的ModelAndView 不能直接使用,應為它沒有指定view,也沒有綁定對應的model對象。當然,model對象不是必須的,但是view確實必須的。 用這個構造方法構造的實例主要用來在以後往其中加view設置和model對象。

給ModelAndView 實例設置view的方法有兩 個:setViewName(String viewName) 和 setView(View view)。前者是使用view name,後者是使用預先構造好的View對象。

其中前者比較常用。事實上View是一個介面,而不是一個可以構造的具體類,我們只能通過其他途徑來獲取 View的實例。對於view name,它既可以是jsp的名字,也可以是tiles定義的名字,取決於使用的ViewNameResolver如何理解這個view name。

如何獲取View的實例以後再研究。 而對應如何給ModelAndView 實例設置model則比較復雜。有三個方法可以使用: addObject(Object modelObject) addObject(String modelName, Object modelObject) addAllObjects(Map modelMap) ModelAndView 可以接收Object類型的對象,ModelAndView 將它視為其眾多model中的一個。

當使用Object類型的對象的時候,必須指定一個名字。ModelAndView 也可以接收沒有明顯名字的對象,原因在於ModelAndView 將調用spring自己定義的Conventions 類的.getVariableName()方法來為這個model生成一個名字。

閱讀全文

與javarender相關的資料

熱點內容
怎樣製作文件夾和圖片 瀏覽:58
調研編譯寫信息 瀏覽:859
python馮諾依曼 瀏覽:417
同時安裝多個app有什麼影響 瀏覽:252
奧術殺戮命令宏 瀏覽:182
用sdes加密明文字母e 瀏覽:359
單片機原理及應用試題 瀏覽:423
易語言開啟指定文件夾 瀏覽:40
馬思純參加密室大逃脫 瀏覽:322
文件夾冬季澆築溫度 瀏覽:712
京東有返點的aPp叫什麼 瀏覽:603
如何查看u點家庭伺服器是幾兆 瀏覽:262
python應用介面怎麼接 瀏覽:67
腐蝕怎麼進不去伺服器啊 瀏覽:359
linuxcpiogz 瀏覽:630
安卓中的布局是什麼文件 瀏覽:397
dex反編譯部分代碼無法查看 瀏覽:464
linuxandroid編譯 瀏覽:603
程序員電視劇20集 瀏覽:910
怎麼擴建文件夾 瀏覽:160